describe('test', () => {
it('test 1', () => {
expect({ data:undefined }).toMatchObject({
data: new Map([['key', 'value']]),
});
});
it('test 2', () => {
expect({ data: ['abcd'] }).toMatchObject({
data: new Map(),
});
});
});
The output:
PASS src/App.test.js
test
✓ test 1 (3 ms)
✓ test 2
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.802 s, estimated 2 s
Ran all test suites matching /src\/App.test.js/i.
I'm writing a test case which does api calls and do a toMatchObject, but even though data is undefined, the test case passed which is very weird. Can anyone explain why these test cases passed? FYI: with toEqual both test cases will fail.
This is mostly an unresolved issue with jest when using Maps or Sets for test cases. Your best bet would be to use objects or convert them to objects via Object.fromEntries and try the tests to get around it but there is no definite fix for it as of now.