Skip to content

Instantly share code, notes, and snippets.

@mbjelac
Created November 29, 2018 09:54
Show Gist options
  • Save mbjelac/d4a549446a15ab3dd926d606b7be9ba2 to your computer and use it in GitHub Desktop.
Save mbjelac/d4a549446a15ab3dd926d606b7be9ba2 to your computer and use it in GitHub Desktop.
Jest toEqual failure report with undefined properties
const a = { foo: undefined, bar: 1 };
const b = { bar: 2 };
test('toEqual wrongly reports undefined properties', () => {
expect(a).toEqual(b);
});
test('toStrictEqual wrongly reports undefined properties', () => {
expect(a).toStrictEqual(b);
});
@mbjelac
Copy link
Author

mbjelac commented Nov 29, 2018

both of these tests correctly fail. the problem is in the failure report:

Error: expect(received).toEqual(expected)

Expected value to equal:
  {"bar": 2}
Received:
  {"bar": 1, "foo": undefined}

Difference:

- Expected
+ Received

  Object {
-   "bar": 2,
+   "bar": 1,
+   "foo": undefined,
  }

the report should have "foo": undefined in neither section because that property is equal to the other object's (since not setting a property and setting it to undefined is the same thing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment