New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

toEqual does not deep equal check custom error objects #6575

Open
anandnimkar opened this Issue Jun 29, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@anandnimkar

anandnimkar commented Jun 29, 2018

馃悰 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 
@anandnimkar

This comment has been minimized.

anandnimkar commented Jun 29, 2018

toStrictEqual also fails in the deep equality comparison

@anandnimkar

This comment has been minimized.

anandnimkar commented Jun 29, 2018

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

@richardscarrott

This comment has been minimized.

Contributor

richardscarrott commented Nov 28, 2018

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment