JavaScript JavaScript Unit Testing Improving Our Tests Covering Edge Cases

Alejandro Molina
Alejandro Molina
3,997 Points

The test worked even though the actual code didn't test for errors yet, unlike what shows in the video

 it('should throw an error if placeShip is missing a parameter e.g. direction', function() {
      var handler = function () {placeShip(player, ship, coordinates); };
      expect(handler).to.throw(Error);
 });

I added this code to the test file and ran it but all 7 tests actually passed without editing the player_methods file to make it throw an error first. Did this happen for anyone else?

Cormac Chisholm
Cormac Chisholm
5,286 Points

I had this happen as well? I'm assuming that the error handling has been added to the player_methods.js file already despite having not been done in the video? Would still like to have that assumption confirmed please. :)

2 Answers

Charles-Antoine Francisco
Charles-Antoine Francisco
27,426 Points

This happens because the ship and coordinates variables are undefined.

Here's a quick fix.

    it('should throw an error if no direction is specified', function () {
      var handler = function() {
        placeShip(player, player.ships[0], [0, 1]);
      };
      // ...
    });
Tom Geraghty
Tom Geraghty
24,159 Points

It is going to throw an error when you attempt to call the function with undefined variables here:

function () {placeShip(player, ship, coordinates)

Since those parameters are undefined the function returns an Error. Which, according to the test suite, is the right behavior but for the wrong reason. This is a pitfall that maybe could have been mentioned in the video: if you're asking the wrong question it doesn't really matter what the answer is. If your testing the wrong functionality, then the results of the tests aren't all that useful.

Gabbie Metheny
Gabbie Metheny
33,703 Points

Around the 7:30 mark, Guil copies and pastes the ship and coordinates variables from the previous spec, which is easy to miss. Like Charles-Antoine Francisco said in his answer, leaving out these arguments will cause an error to be thrown, just not the one you want. So, the test passes. This is what Guil's code looks like before running the initial test spec:

it('should throw an error if no direction is specified', function() {
  // code Guil copied and pasted from previous spec
  var ship = player.ships[0];
  var coordinates = [0, 1];

  var handler = function() { placeShip(player, ship, coordinates); };
  expect(handler).to.throw(Error);
});

Guil does show later in the video how you can specify what type of error you're expecting, though, so having a check like that in place will help with ensuring you're actually handling your errors, not just creating new errors due to a bad test spec!

// pass type of error or text of error message to throw()
expect(handler).to.throw('You left out the direction! I need that for math!');

If the handler doesn't throw the correct error message because you didn't define ship and coordinates, you'll get an AssertionError when you run your test, telling you the actual error was 'coordinates is not defined'.