Jest: toEqual does not deep equal check custom error objects

Created on 29 Jun 2018  路  3Comments  路  Source: facebook/jest

馃悰 Bug Report

toEqual does not deep equal check custom error objects correctly.

To Reproduce

Refer to the included code snippet. It's self-explanatory.

Expected behavior

Error objects with custom fields should be deep checked.

Link to repl or repo (highly encouraged)

https://gist.github.com/anandnimkar/1c45c453a7edb9df0b3eb6d2a1cb9fa7

Issues without a reproduction link are likely to stall.

Run npx envinfo --preset jest

Paste 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 

Most helpful comment

Here is a temporary workaround if anyone google's this: expect({...error1}).toEqual({...error2});

All 3 comments

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);
Was this page helpful?
0 / 5 - 0 ratings