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.

Tests after upgrading [react-scripts] to version 4 fail

See original GitHub issue

Describe the bug

After updating react-scripts to version 4+ more than half of the tests started to fail. But if I run any of the failing tests in VSCode or from jest they don’t fail and work as before.

Looks like any of the mocks I use in my ./src/setupTests.ts file aren’t picked up when I run react-scripts test.

Did you try recovering your dependencies?

Yes. I did.

Which terms did you search for in User Guide?

They are not related to the issue.

Environment

System: OS: macOS 11.1 CPU: (8) x64 Intel® Core™ i7-4750HQ CPU @ 2.00GHz Binaries: Node: 15.8.0 - /usr/local/bin/node Yarn: 1.22.10 - /usr/local/Cellar/node/15.8.0/bin/yarn npm: 7.5.0 - /usr/local/Cellar/node/15.8.0/libexec/bin/npm Browsers: Chrome: 88.0.4324.182 Firefox: Not Found Safari: 14.0.2 npmPackages: react: ^17.0.1 => 17.0.1 react-dom: ^17.0.1 => 17.0.1 react-scripts: ^4.0.2 => 4.0.2 npmGlobalPackages: create-react-app: 4.0.2

Steps to reproduce

Expected behavior

Tests shouldn’t fail and work the same way as they work using Jest

Actual behavior

Failing test which uses mocked localStorage: Screenshot 2021-02-17 at 17 15 10

Failing test which uses mocked version of window.open: Screenshot 2021-02-17 at 17 16 17

Failing test which uses redux (I don’t mock redux, use the same createStore function which already has thunk middleware): Screenshot 2021-02-17 at 17 17 55

setupTests.ts

import 'jest-localstorage-mock';
import '@testing-library/jest-dom';
import 'jest-date-mock';
import './test-helpers/react-i18next.mock';

window.scrollTo = jest.fn();
window.open = jest.fn();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

37reactions
wamujlbcommented, Feb 24, 2021

Found an issue. From 4+ version of react-scripts --resetMocks is true by default. Fixed it by making react-scripts test --resetMocks=false.

0reactions
ozluycommented, Oct 22, 2021

--resetMocks=false flag and override with react-app-rewired worked for me

// config-overrides.js
module.exports.jest = (config) => {
  config.testRunner = 'jest-jasmine2';
 return config;
};```
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Tests Failing After Updating React Scripts from v.3.4.4 to v ...
I went back through the project and isolated the breaking change to be the update from react-scripts 3.4.4 to 4.0.0. Basically, the main...
Read more >
Upgrade the React App Version - The newline Guide to ...
Step one: Go to the changelogs for the React Scripts, and find the newest version of the react-scripts available. At the time of...
Read more >
Testing with react-scripts does not work since update - YouTrack
Testing stopped working after upgrade, i downloaded the previous version to check and testing still works there. No extra settings for the test...
Read more >
react-scripts | Yarn - Package Manager
We've also updated our templates to use createRoot and relaxed our check for older versions of Create React App. Migrating from 5.0.0 to...
Read more >
How to fix broken Jest in React 16.4.1 with CRA and debug ...
Turns out it's something with the react-scripts portion of create-react-app being out of date on its version of the Jest Testing API. I...
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