question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jest 25 invokes setters during toEqual comparison, causing side effects

See original GitHub issue

This 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Apr 2, 2020

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

1reaction
SimenBcommented, Apr 2, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found