Exercises: Unit Testing
In many of your previous coding tasks, you had to verify that your code
worked before moving to the next step. This often required you to add
console.log() statements to your code to check the value stored in a variable
or returned from a function. This approach finds and fixes syntax, reference,
or logic errors AFTER you write your code.
In this chapter, you learned how to use unit testing to solve coding errors. Even better, you learned how to PREVENT mistakes by writing test cases before completing the code. The exercises below offer practice with using tests to find bugs, and the studio asks you to implement TDD.
For the exercises, open javascript-projects/unit-testing/exercises to find the files you will need to get started.
Automatic Testing to Find Errors
Let’s begin with the following code in checkFive.js:
| |
The function checks to see if a number is greater than, less than, or equal to 5. We do not really need a function to do this, but it provides good practice for writing test cases.
Note that the exercises directory also contains a tests directory for us.
We need to add a few lines to
checkFive.jsandcheckFive.test.jsto get them to talk to each other.checkFive.test.jsneeds to accesscheckFive.js. Add arequirestatement to accomplish this.Make the
checkFivefunction available to the spec file, by usingmodule.exports.
Set up your first test for the
checkFivefunction. In thecheckFive.test.jsfile, add adescribefunction with onetestclause:1 2 3 4 5 6 7 8 9const checkFive = require('../checkFive.js'); describe("checkFive", function(){ test("Descriptive feedback...", function() { //code here... }); });Now write a test to see if
checkFiveproduces the correct output when passed a number less than 5.First, replace
Descriptive feedback...with a DETAILED message. This is the text that the user will see if the test fails. Do NOT skimp on this. Refer back to Specifications and Expectations section to review best practices.Define the variable
output, and initialize it by passing a value of2tocheckFive.1 2 3 4 5 6 7 8 9const checkFive = require('../checkFive.js'); describe("checkFive", function(){ test("Descriptive feedback...", function(){ let output = checkFive(2); }); });Now use the
expectfunction to check the result:1 2 3 4 5 6 7 8 9 10const checkFive = require('../checkFive.js'); describe("checkFive", function(){ test("Descriptive feedback...", function(){ let output = checkFive(2); expect(output).toEqual("2 is less than 5."); }); });Run the test script and examine the results. The test should pass.
Now change line 3 in
checkFive.jstoif (num > 5)and rerun the test.Change line 3 back.
NoteWe do NOT need to check every possible value that is less than 5. Testing a single example is sufficient to check that part of the function.
Add two more
testclauses insidedescribe—one to test what happens whencheckFiveis passed a value greater than 5, and the other to test when the value equals 5.
Try One on Your Own
Time for Rock, Paper, Scissors! The function in RPS.js takes the choices
('rock', 'paper', or 'scissors') of two players as its parameters.
It then decides which player won the match and returns a string.
| |
Set up the
RPS.jsandRPS.spec.jsfiles to talk to each other.Write a test in
RPS.test.jsto check ifwhoWonbehaves correctly when the players tie (both choose the same option). Click “Run” and examine the output. SPOILER ALERT: The code for checking ties is correct inwhoWon, so the test should pass. If it does not, modify yourteststatement.Write tests (one at a time) for each of the remaining cases. Run the tests after each addition, and modify the code as needed. There is one mistake in
whoWon. You might spot it on your own, but try to use automated testing to identify and fix it.
Bonus Mission
What if something OTHER than 'rock', 'paper', or 'scissors' is
passed into the whoWon function? Modify the code to deal with the
possibility.
Don’t forget to add another test clause in RPS.test.js to test for this
case.