JavaScript

Testing exceptions in JavaScript with Jest

The code

Let’s consider a simple function that checks for equality of two passwords, and it throws an error when the first one is not provided:

export default function samePasswordsValidator(password, otherPassword) {
    if (!password) {
        throw new Error("no password given");
    }
    return password === otherPassword;
}

Try-catch idiom (bad)

When testing the code that throws exceptions, one immediately comes up with the idea of using the try-catch idiom in the test code:

it('throws an error when first argument is `null`', () => {
    try {
        samePasswordsValidator(null, "bar");
    } catch (error) {
        expect(error.message).toBe("no password given");
    }
});

Generally speaking, this is not the best approach. The test passes when the first argument is null as expected. But when the code is about to change and the exception won’t be thrown anymore the test still passes. So, the code change won’t be detected by the test.

Try-catch idiom (better)

To overcome that issue, one could expect that actual assertion will be executed and fail the test if it does not happen. This can be done pretty easily with expect.assertions which verifies that a certain number of assertions are called during a test:

it('throws an error when first argument is `null`', () => {
    expect.assertions(1);
    try {
        samePasswordsValidator(null, "bar");
    } catch (error) {
        expect(error.message).toBe("no password given");
    }
});

Now, when no exception is thrown, the test fails:

Error: expect.assertions(1)

Expected one assertion to be called but received zero assertion calls.

toThrow assertions (best)

To make the code even more expressive a built-in toThrow matcher can be used:

it('throws an error when first argument is `null`', () => {
    expect(() => samePasswordsValidator(null, "bar")).toThrow("no password given");
});

And again, when no exception is thrown, Jest informs us clearly about it with a failed test:

Error: expect(received).toThrow(expected)

Expected substring: "no password given"

Received function did not throw

Note that toThrow matcher can be used to not only check the error message, but also the exact type of the error:

it('throws an error when first argument is `null`', () => {
    expect(() => samePasswordsValidator(null, "bar")).toThrow(Error);
    expect(() => samePasswordsValidator(null, "bar")).toThrow(new Error("no password given"));
});

Published on Java Code Geeks with permission by Rafal Borowiec, partner at our JCG program. See the original article here: Testing exceptions in JavaScript with Jest

Opinions expressed by Java Code Geeks contributors are their own.

Rafal Borowiec

Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button