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
- fix(merge): - split the implem into merge(target, ...sources) and _merge(target, source), I think it's more simple this way. - I reorganized some of the conditions - I mostly kept the description and... — committed to algolia/algoliasearch-helper-js by tkrugg 5 years ago
- fix(merge): use .toStrictEqual instead of .toEqual in tests cf. https://github.com/facebook/jest/issues/711 `undefined` values being critical to in assessing merge behaviour this is necessary — committed to algolia/algoliasearch-helper-js by tkrugg 5 years ago
- fix(merge): change implementation (#716) * fix(merge): use .toStrictEqual instead of .toEqual in tests cf. https://github.com/facebook/jest/issues/711 `undefined` values being critical to in ... — committed to algolia/algoliasearch-helper-js by tkrugg 5 years ago
- fix(merge): change implementation (#716) * fix(merge): use .toStrictEqual instead of .toEqual in tests cf. https://github.com/facebook/jest/issues/711 `undefined` values being critical to in ... — committed to algolia/algoliasearch-helper-js by tkrugg 5 years ago
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:
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 strangeJSON.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.