jest: toEqual ignores keys with undefined properties

If a property is undefined, Jest appears to ignore it when calling .toEqual().

import _ from "lodash"; describe("test", () => { it("returns an object with the correct nodes", () => { let a = {"key1": undefined, "key2": "a"}; let b = {"key2": "a"}; expect(a).toEqual(b); expect(_.isEqual(a, b)).toBeFalsy(); }); });

This simple test will pass. As you can see by adding lodash, it should be false.

My output: Using Jest CLI v0.8.2, jasmine1 Running 1 test suite… PASS processors\hockey__tests__\events.js (0.573s) 1 test passed (1 total in 1 test suite, run time 1.076s) ----------|----------|----------|----------|----------|----------------|

File % Stmts % Branch % Funcs % Lines Uncovered Lines
---------- ---------- ---------- ---------- ---------- ----------------
All files 100 100 100 100
---------- ---------- ---------- ---------- ---------- ----------------

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 7
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Is anyone concerned with this? I got a bug today because of this issue.

It might help if the docs for toEqual mentioned the ways in which it differs from toStrictEqual instead of you having to discover toStrictEqual on your own.

This is sorta fixed, we have toStrictEqual now.

EDIT: based on this comment, I’ll call this issue fixed:

would be impossible to roll out at FB. Having a separate strict matcher in Jest is the better option.

Slightly a footgun that’s it’s not the default, but legacy.

If you want to enforce usage in your code base, you can use https://github.com/jest-community/eslint-plugin-jest/pull/134, which can autofix your code

Hey guys, what about adding a feature switch for us open source users to use isEqual() without strange JSON.stringify() related surprises?

@SimenB For now I have a couple workarounds (e.g. using toHaveProperty) but I was hoping there was some timeline on a fix or at the least a note in the docs about this issue.