@neomaventures/matchers
v0.1.0
Published
Jest matchers for asserting an error's class and its properties in one assertion — toThrowMatching and toMatchError.
Maintainers
Readme
@neomaventures/matchers
Custom Jest matchers for asserting on thrown errors, self-registering via setupFilesAfterEnv.
Provides toThrowMatching and toMatchError — both assert that an error is an
instance of a given class and carries the properties you expect, which
Jest's built-in toThrow cannot do in one assertion.
Installation
npm install --save-dev @neomaventures/matchersRegister it in your Jest config. The import has no exports — it calls
expect.extend() on load and augments the jest.Matchers interface, so the
matchers and their types are available everywhere without importing them
per file.
{
"setupFilesAfterEnv": ["@neomaventures/matchers"]
}Usage
{
"setupFilesAfterEnv": ["@neomaventures/matchers"]
}Then use in tests:
// Check that a function throws a specific error class
expect(() => service.register(email)).toThrowMatching(
EmailAlreadyExistsException,
)
// Check class + properties
expect(() => service.register(email)).toThrowMatching(
EmailAlreadyExistsException,
{ email: '[email protected]' },
)
// Check an already-caught error
expect(caughtError).toMatchError(NotFoundException, { message: 'Not found' })License
MIT
