Test Driven Development (TDD)

Usually the first section contains a few questions for you to answer to make sure you understand the concepts from the previous chapter. Starting from this chapter, there will be no more preflight questions. At this point, you should be mature enough to review previous concepts frequently. Instead, you'll warm up this time by learning more about testing and writing some functions that you'll use later.

Because testing your code is so important, it has become common to practice Test Driven Development (TDD).

TDD is the concept of writing all your tests first and then writing your code. Writing with tests in mind helps you write code faster, more efficiently, and more cleanly by giving you a clear target of what you're developing towards. Additionally, it can be easier to write a more complete set of tests before you've started coding, because your mind is still focused on what the program should do, not what it can do.

So far we've been providing tests for each exercise. Starting with this chapter you'll be writing your own tests! This section will walk you through the process of TDD for a few functions.

  1. Create a test file called helpers.test.js.

  2. Write the test code. You'll recognize the functions from the tests we've written for you.

    const helpers = require('./helpers')
    /*
     When we run our file with the jest framework, jest provides us
        with a few functions. The function our tests will go inside
        is describe, which takes in 2 arguments (string and function).
    */
    // First argument describes the function you are testing;
    //   second argument is a function that contains all the tests.
    describe('sumAll function', () => {
    	// For each test, you need to run the `it` function,
      //    which takes in 2 arguments (string and function).
      
    	// First argument describes the test;
      //    second argument is a function that runs the test.
    	it('should add numbers', () => {
        // Run the function we want to test
    		const result = helpers.sumAll([9,8,7])
    
    		// Pass the result from the function into expect
        // expect returns an object with a toEqual key;
        //    pass what you expect into the `toEqual` function.
        expect(result).toEqual(24)
      })
    })
    

    Exercise: Write two more tests. During interviews or at work try to have at least 3 examples in each test.

  3. Like you learned in JS2, create a solution file called helpers.js that exports an object:

    const allFuns = {}
    
    allFuns.sumAll = (arr) => {
      return 0
    }
    
    module.exports = allFuns
    
  4. Now that you have all the files set up, you can run your test! You should see some failures because your function is not yet returning the correct result. As your code moves toward completion, you'll see more and more tests pass.

  5. Exercise: Write the function so that all the tests pass.

<aside> 💡 TDD is a process where you write the test first, then write the function, like the example above. TDD is very common practice among developers.

</aside>

It is best practice to have each function you want to test contained in its own describe code block. If you wanted to test another function (like findLargest), you can add it onto your test file:

const helpers = require('./helpers')
 
describe('sumAll function', () => {
   ...
})

describe('findLargest number', () => {
  it('should find the largest number from array with all negatives', () => {
		const result = helpers.findLargest([-10, -1, -19])
    expect(result).toEqual(-1)
  })
  ...
})

Exercise: Write the findLargest function in helpers.js