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 | |
----------|----------|----------|----------|----------|----------------|
this is coming from here. @cpojer is there a reason why that check can't be removed? all the tests on the jest repository pass without it.
@jamedranoa yes, I plan on eventually removing this check. Unfortunately we have a shitload of tests at FB that depend on this behavior. My goal is to unfork jasmine eventually though, it'll happen in the next few months.
Cool thank you! :+1:
@cpojer Are there still plans to remove this?
@scsper we still have plans, but there's still too many FB tests that depend on this behavior, so it's not going to happen soon :/
Hey guys, what about adding a feature switch for us open source users to use isEqual() without strange JSON.stringify() related surprises?
Is there a workaround for this?
@cpojer any updates on this issue? I ran into it recently and noticed this issue has gone stale.
You can create your own matcher using lodash.toEqual if you want
@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.
Hmmm... the issue says it ignores undefined. I'm seeing the opposite - it does not ignore undefined properties. It seems to depend on which side goes into expect() and vs toEqual().
Wish there was a way to toggle the behavior. In my case I want it to ignore undefined as JSON.stringify will strip undefined properties anyway (so I don't care).
@lukescott
what did your test case case look like?
Im seeing undefined gets ignored no matter which side the toEquals it is in.
Is anyone concerned with this? I got a bug today because of this issue.
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
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.
Most helpful comment
Is anyone concerned with this? I got a bug today because of this issue.