index
Learn about test-driven development and practice JS array methods
Test-driven development (TDD) is a methodology where you write tests before you write any code. This forces you to think through exactly how your code should behave. It's kind of like planning an essay before you start writing it. The iterative process of writing each test is supposed to help with solving a problem too.
TDD process
TDD generally follows the "red, green, refactor" cycle.
Red
Write a test that fails. This is important: if you never see your test fail you might have a false positive (a test that passes even if your code is broken).
Green
Write as little code as possible to make the test pass. Make sure you don't break any existing tests.
Refactor
Change your code to improve it (if necessary). You have passing tests to tell you if you break anything.
Repeat
Go through the cycle until you think you have a complete working solution
TDD example
Let's run through the process by creating a double
function using TDD. First we write a failing test:
Then we write as little code as we need to make the test pass:
This will feel a bit contrived for a problem where we already know what the final code should be. The idea is not to try and solve the whole problem in one go—TDD is a way to help you solve a harder problem by iterating through solutions.
Then we refactor, if needed. Since we can't make this any simpler let's keep going and repeat the cycle. We need another failing test:
Once we see that fail we can amend our function to make it pass:
Once the test passes we can try to refactor our function to remove repetition. Instead of listing every possible input/output, we can see that we need to return the input multiplied by two each time.
This solution looks complete, so we can end the TDD cycle here. It might be worth adding more tests for edge-cases (e.g. what happens when you don't pass any argument), but TDD has helped us solve the problem itself.
If you're confused about the TDD process at the end of the workshop there's a fully explained starter-files/solution/tdd-explanation.js
version of the solution that walks through the process step-by-step.
Workshop
We're going to re-create some useful JavaScript array methods using TDD. For example if we're re-creating the array.map
method we should use other JS features (like for
loops) to create a function that does the same thing as .map
, without using .map
itself.
For each method you should use TDD to write tests first, then write the actual code. Work in pairs and alternate: person 1 writes a test, then person 2 makes it pass. Then person 2 writes the next test and person 1 makes that pass.
Setup
Clone this repo
Open
index.html
in your browserAlternate writing tests and code in
index.test.js
andindex.js
You can see test results in the console
map
map
Use TDD to write your own map
function that behaves like the built-in one. The only difference should be that yours takes the array as the first argument:
There is one passing test and one failing test to get you started.
filter
filter
Use TDD to write your own filter
function that behaves like the built-in one. The only difference should be that yours takes the array as the first argument:
every
every
Use TDD to write your own every
function that behaves like the built-in one. The only difference should be that yours takes the array as the first argument:
some
some
Use TDD to write your own some
function that behaves like the built-in one. The only difference should be that yours takes the array as the first argument:
find
find
Use TDD to write your own find
function that behaves like the built-in one. The only difference should be that yours takes the array as the first argument:
reduce
reduce
The function is called with the current value of the accumulator and the current element. Whatever you return from the function is used as the accumulator value for the next iteration. After the final element the final accumulator value is returned.
Use TDD to write your own reduce
function that behaves like the built-in one. The only difference should be that yours takes the array as the first argument:
Stretch goal: flat
flat
Use TDD to write your own flat
function that behaves like the built-in one. The only difference should be that yours takes the array as the first argument:
Hint: recursion or while
loops will be helpful.
Last updated