toEqual does not deep equal check custom error objects correctly.
Refer to the included code snippet. It's self-explanatory.
Error objects with custom fields should be deep checked.
https://gist.github.com/anandnimkar/1c45c453a7edb9df0b3eb6d2a1cb9fa7
Issues without a reproduction link are likely to stall.
npx envinfo --preset jestPaste the results here:
System:
OS: macOS Sierra 10.12.6
CPU: x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Binaries:
Node: 10.1.0 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.1.0 - /usr/local/bin/npm
npmPackages:
@types/jest: ^23.1.0 => 23.1.1
jest: ^23.1.0 => 23.1.0
toStrictEqual also fails in the deep equality comparison
Here is a temporary workaround if anyone google's this: expect({...error1}).toEqual({...error2});
I'd be interested to know why toEqual has a special case for Error objects, it seems so bizarre -- perhaps a legacy from jasmine?
As an FYI, the spread hack works for simple Error objects however it fails when using libraries like VError which house nested errors.
I've settled for lodash toEqual for now -- although it's a shame there's no pretty printing on errors e.g.
expect(
isEqual(
actual,
new MultiError([
new VError(
{ info: { id: 1, cause: ex } },
'Missing shipping method for seller'
),
new VError(
{ info: { id: 2, cause: ex } },
'Missing shipping method for seller'
),
])
)
).toBe(true);
Most helpful comment
Here is a temporary workaround if anyone google's this:
expect({...error1}).toEqual({...error2});