Skip to content

Latest commit

聽

History

History
37 lines (24 loc) 路 1.01 KB

no-error-equal.md

File metadata and controls

37 lines (24 loc) 路 1.01 KB

jest/no-error-equal

馃摑 Disallow using equality matchers on error types.

馃挱 This rule requires type information.

When comparing errors, toEqual and toStrictEqual will only compare the message properties, meaning tests can pass even if the errors are of different types.

Instead, it is better to use toThrow which does check the error type along with its message.

Rule details

This rule warns when toEqual and toStrictEqual is used with an Error type.

The following patterns are considered warnings:

expect(new AggregateError([], expect.any(String))).toEqual(
  new Error(expect.any(String)),
);

expect(new Error('hello world')).toStrictEqual('hello sunshine');

The following patterns are not considered warnings:

expect(() => throw new AggregateError([], expect.any(String))).toThrow(new Error(expect.any(String)));

expect(() => throw new Error('hello world')).toThrow('hello sunshine');