Home » JavaScript » Testing exceptions in JavaScript with Jest

About Rafal Borowiec

Rafal Borowiec
Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.

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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments