Skip to main content

How to Test Exception Messages with MochaJS

In this article I show you how to test exception messages with MochaJS.

Step 1. Clone the starter project

Use git to clone my starter repo into a new folder.

git clone https://github.com/mitchallen/autom8able-mochajs-starter.git mocha-awesome
cd mocha-awesome/
npm install

Run the default tests to make sure the install went okay:

npm test

Step 2. Add a test for an exception message

The starter project contains a class that has a greet method.

Let’s add a new requirement that the greet method will throw an exception if the greeting string is an empty string. As part of the requirement, the error message in the exception must contain the string ’empty string’.

Edit test/smoke-test.js and add this test under the context for greeting method:

it('should throw an exception for empty string', done => {
var hello = helloFactory.createHelloWorld();
hello.greeting = ''; // Set to empty string
assert.throws(
() => hello.greet(),
/empty string/
);
done();
});

The test will set the greeting string to an empty string in an attempt to trigger the error.

The assert.throws call does the following:

  • wraps a call to hello.greet
  • tests that an error was thrown where the message matches the regex empty string

Since we haven’t modified the greet method yet, the test will fail because the exception won’t be thrown. To see what that looks like, run the tests now:

npm test

The results should look like this:

  smoke test
✓ should pass if mocha is working
createHelloWorld
✓ should return an object that is not null
HelloWorld
greeting property
✓ should have a greeting property
✓ should return default greeting if not set
✓ should return set value
✓ should return constructor greeting if set
greet method
✓ should have a greet method
Hello World!
✓ should be able to call greet method
Yo World!
✓ should be able to call greet method with new greeting

1) should throw an exception for empty string

9 passing (11ms)
1 failing

1) smoke test
HelloWorld
greet method
should throw an exception for empty string:
AssertionError [ERR_ASSERTION]: Missing expected exception.

The test failed because the exception was not thrown.

Step 3. Add the new exception to the method

Edit src/index.js and update the greet method with this code:

greet() {
if( this.greeting.length === 0 ) {
throw new Error('Greeting can not be empty string.')
}
console.log(this.greeting);
}

Run the tests again and all the tests should pass.

Step 4. Test what happens if the strings don’t match

Change the regex string value in the test case to something that won’t match:

it('should throw an exception for empty string', done => {
var hello = helloFactory.createHelloWorld();
hello.greeting = ''; // Set to empty string
assert.throws(
() => hello.greet(),
/small string/
);
done();
});

Run the test again and you should see a test fail with this message:

AssertionError [ERR_ASSERTION]: The input did not match the regular expression /small string/. Input:

'Error: Greeting can not be empty string.'

Restore the regex to /empty string/ and the test should pass again.

Troubleshooting

A common mistake with assert.throws is to forget to call the target method within an arrow function.

Conclusion

In this article you learned how to:

  • Use assert.throws to test for an exception
  • Update a method to throw an exception to match a test requirement
  • Analyze the results when an expected exception does not occur

References

  • assert.throws [1]
  • mochajs.org [2]