npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-error-matcher

v0.1.0

Published

A jest extension to provide better matching against errors.

Downloads

5

Readme

jest-error-matcher


Provides a custom matcher for Jest that does more rigorous matching on errors and, most importantly, better error reporting when the match fails.

This is especially useful when your error objects have attached properties; it can be very difficult with Jest's default diffing and output to know where they failed to match.

Overview

import "jest-error-matcher";

/* import your module under test */

it("should throw an error with useful properties attached", () => {
    expect(myFunctionUndertest).toThrowErrorMatching({
        name: "MyError",
        message: expect.stringMatching(/something went wrong with code E[0-9]+/),
        code: expect.stringMatching(/^E[0-9]+/$)
    });
});

it("should rejet with a useful Error", () => {
    expect(myAsyncFunctionUnderTest).rejects.toThrowErrorMatching({
        name: "MyError",
        /* ... */
    });
});

Matchers Provided

When imported, this package adds the following matchers to Jest's expect object:

  • toThrowErrorMatching(expected?) - This acts similar to the Jest default toThrow matcher, expecting the subject to be a function which throws an error.
  • toBeErrorMatching(expected?) - This applies the same matcher rules as toThrowErrorMatching, but instead of expecting a function to throw an error, it expects the subject to be the Error object itself.

Both matchers take an optional object against whose properties the error is matched. Additionally, the matchers expect that the error is an instance of the Error class. If no argument is given, the matchers will only verify that the error is an instance of Error.

Only those own-properties that are specified for expected will be subject to the match. Any properties on the error that are not included in expected will be ignored.

Negation

These matchers do no support negation (e.g., expect(...).not.toBeErrorMatching(...) will throw an Error). This is because it's somewhat ambiguous to say we expect a function to not throw an error matching an expected value: do you mean it shouldn't throw any error or that it should throw an error, but one that doesn't match?

Value Added over toThrow

We've learned that rich errors can be very helpful, where the Error object is adorned with properties providing details and context in a machine-readable way. These properties allow for easier and safer error-handling and monitoring than trying to parse information from an error message. However, the built in toThrow matcher for Jest (and it's alias toThrowError) doesn't support this well. You can use assymetric matchers like expect.objectContaining to match against custom properties, but when your expectation fails, the output is not terribly helpful. Specifically, it doesn't show you any custom properties of the error, and it doesn't show a diff the way other object matchers do.

For instance, using the built-in toThrow matcher...

expect(() => {
  throw Object.assign(new Error("Wrong error message"), {
    foo: "wrong foo value",
  });
}).toThrowError(
  expect.objectContaining({
    foo: "Correct foo value",
    message: "Correct error message",
  })
);

...you get the following output:

toThrow matcher output

Using the custom matchers provided by the this package...

expect(() => {
  throw Object.assign(new Error("Wrong error message"), {
    foo: "wrong foo value",
  });
}).toThrowErrorMatching({
  foo: "Correct foo value",
  message: "Correct error message",
});

...you get much more useful output:

toThrowErrorMatching matcher output

Bugs and Pull Requests

Please report bugs on github at mearns/jest-error-matcher/issues.

If you have an enhancement you'd like to make, you can submit a pull request to mearns/jest-error-matcher, but I suggest you open an issue first to discuss the idea before you spend a lot of time on it.