Jest 25 invokes setters during toEqual comparison, causing side effects
See original GitHub issueThis is similar to https://github.com/facebook/jest/issues/9531.
It is not safe to assign a bunch of setters in the passed object. They may throw or they may have side effects. This isn’t theoretical because we may add setters like this to React.
Example: https://repl.it/repls/HuskyCandidConfiguration
test('setter object comparison', () => {
expect({
get a() {
return {};
},
set a(value) {
throw new Error('noo');
},
b: {},
}).toEqual({
a: {},
});
});
This used to work fine in 24: https://repl.it/repls/AllMediumorchidDeals
This regression is currently blocking upgrading React to Jest 25.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Using Matchers - Jest
Using Matchers. Jest uses "matchers" to let you test values in different ways. This document will introduce some commonly used matchers.
Read more >What is the difference between 'toBe' and 'toEqual' in Jest?
Use .toEqual when you want to check that two objects have the same value. This matcher recursively checks the equality of all fields,...
Read more >babel-preset-jest | Yarn - Package Manager
Babel preset for all Jest plugins. This preset is automatically included when using babel-jest. Install. $ npm install --save-dev babel-preset-jest ...
Read more >jest console log after tests are done - You.com | The AI Search ...
I am attempting to run the "stock" test for my service, i.e., the one feathersjs sets up for you when it creates the...
Read more >Comparison of React components testing patterns - Theseus
4.5.1 Shallow mount and mount comparison. 25. 5 Analysing and improving test patterns of case study React application 30. 5.1 Background.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Ooh, just dropping the setter from the descriptor sounds good. We never want to run them anyways, as we only do this stuff after an assertion has failed and we generate a diff
We don’t mutate the actual object passed in - we make a deep copy and mutate that copy. The reason is that we want to replace the properties that use asymmetric matchers and pass with the value, so that it’s not highlighted as failing in the diff view.
It’s not an optimal solution, ideally we’d have a smarter object diffing than a full serialization then comparing strings