react-native: Cannot find module 'setupDevtools' from 'setup.js' in react-native/jest/setup.js version 0.56.0

Environment

node version 8.11.3 (and other windows systems have older yarn and node versions)

  React Native Environment Info:
    System:
      OS: Windows 10
      CPU: x64 Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz
      Memory: 11.10 GB / 15.96 GB
    Binaries:
      Yarn: 1.9.2 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
      npm: 5.6.0 - C:\Program Files\nodejs\npm.CMD
    IDEs:
      Android Studio: Version  3.1.0.0 AI-173.4720617

Description

Since upgrading to react-native 0.56.0, I can no longer run tests on windows. Every test file fails with the error below

FAIL startup/App.spec.js ● Test suite failed to run Cannot find module ‘setupDevtools’ from ‘setup.js’ at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17) at Object.<anonymous> (node_modules/react-native/jest/setup.js:33:6)

Jest Config I think everything below the `setupFiles` was stuff I added that made no effective difference.
{
    preset: 'react-native',
    collectCoverageFrom: [
        '**/*.{js}',
        '!**/node_modules/**',
        '!**/vendor/**',
        '!**/native-base-theme/**'
    ],
    testPathIgnorePatterns: [
        '/node_modules/'
    ],
    transform: {
        '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js'
    },
    transformIgnorePatterns: [
        '/node_modules/(?!native-base)/'
    ],
    setupFiles: [
        './testSetup.js'
    ],
    testRegex: "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    moduleDirectories: [
        'node_modules'
    ]
}
Package.json
{
  "devDependencies": {
    "@babel/core": "7.0.0-beta.47",
    "@babel/plugin-proposal-decorators": "7.0.0-beta.47",
    "@babel/plugin-transform-runtime": "7.0.0-beta.47",
    "@babel/runtime": "7.0.0-beta.47",
    "babel-eslint": "^8.0.1",
    "babel-jest": "^23.4.0",
    "babel-plugin-extensible-destructuring": "4.2.1",
    "babel-plugin-jest-hoist": "^23.2.0",
    "babel-plugin-transform-remove-console": "^6.8.5",
    "babel-preset-react-native": "^5.0.0",
    "enzyme": "^3.3.0",
    "enzyme-adapter-react-16": "^1.1.1",
    "eslint": "^4.10.0",
    "eslint-loader": "^1.9.0",
    "eslint-plugin-babel": "^4.1.2",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-jest": "^21.3.2",
    "eslint-plugin-prettier": "^2.3.1",
    "eslint-plugin-react": "^7.1.0",
    "flow": "^0.2.3",
    "flow-bin": "^0.75.0",
    "jest": "23.4.1",
    "madge": "^3.0.1",
    "patch-package": "^5.1.1",
    "postinstall-prepare": "^1.0.1",
    "prettier-eslint-cli": "^4.6.1",
    "react-dom": "^16.1.1",
    "react-test-renderer": "16.4.1",
    "semver-regex": "^1.0.0",
    "shelljs": "^0.7.8"
  },
  "dependencies": {
    "axios": "^0.17.1",
    "base-64": "^0.1.0",
    "base64-js": "^1.3.0",
    "bcryptjs": "^2.4.3",
    "buffer": "^5.0.8",
    "core-js": "2.5.2",
    "events": "^1.1.1",
    "extensible-runtime": "^4.1.0",
    "he": "^1.1.1",
    "lodash": "^4.17.4",
    "moment": "^2.19.1",
    "moment-timezone": "^0.5.17",
    "prettier": "^1.9.1",
    "prop-types": "^15.6.0",
    "react": "16.4.1",
    "react-native": "0.56.0",
    "react-navigation": "^1.0.0-beta.19",
    "redux": "^3.7.2",
    "redux-saga": "^0.16.0",
    "urijs": "^1.19.0",
    "util": "^0.10.3"
  },
  "resolutions": {
    "core-js": "2.5.2"
  }
}

What I’ve already tried

  • I tried a bunch of things from a few semi-related issues: Jest #3822, stryker-mutator #717, StackOverflow A, B, C
  • Tried upgrading to the latest yarn, and node.
  • Tried many changes to my jest config based on the suggestions above
  • Tried throwing away my yarn.lock file so it would grab the latest version of everything subject to package.json
  • The repo below and snippets above are vastly stripped down from the original versions that I started with
  • Tried installing babel-core@7.0.0-bridge.0 as a dev dependency since yarn complains about it being a missing peer dependency

Things that affect the error:

If I remove the transform section of the jest config then the setup.js error goes away, but the “Plugin 0” error occurs (on macOs too):

Plugin 0 specified in “/Users/me/dev/myApp/node_modules/babel-preset-react-native/index.js” provided an invalid property of “default” (While processing preset: “…”)

If I add the following to my jest config, the error changes to the error below

    rootDir: '.',
    roots: [
        '<rootDir>'
    ],

FAIL startup/App.spec.js ● Test suite failed to run Reference Error babelHelpers is not defined

1 | import App from ‘./App’; | ^ 2 | 3 | describe(‘App’, () => { 4 | it(‘Will render’, () => { at Object.<anonymous> (startup/App.spec.js:1:52)

Reproducible Demo

See: this repo for a reproducible (on windows only) example.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 30
  • Comments: 36 (1 by maintainers)

Most upvoted comments

If you see this issue it’s due to this line in the react-native jest setup file:

jest.mock('setupDevtools');

The error is because for some reason Jest cannot see the setupDevtools module. To fix, verify that this file is actually in the project, and if it is then your haste/metro cache must be invalid and can’t see it

To fix you can run @lyoss’s command to reset all the caches

For me the issue was watchman – running the tests with the --no-watchman flag worked.

In my case, .watchmanconfig was ignoring node_modules

I can confirm the master branch of react-native has the issue resolved. I’ve also narrowed down the exact commit that fixed things - namely 3ac86c3.

Lately, I’ve been using patch-package to patch things that need fixing - especially when the fixes are small and patches are easy to comprehend. You can see that patch in action in the latest commit in my repro repo.

FYI if you are working with a monorepo that has a mix of react-native and react-native-web then you might need to add "nohoist": ["**/react-native", "**/react-native/**"] to the main package.json(at the root of the monorepo)

I tried with different version of react native but not able to resolve this issue. It is not a Jest issue . It is related to react-native. Please help if anyone have a solution. I am getting same issue in react-native version 0.55.4 also.

That eject-part is to re-create as near as possible to our own project

Was your project created via create-react-native-app? Because I’m concerned that running eject in a non-CRNA project could lead to weird side effects… or do nothing 😅

Uhm ok I’m still confused, why did you run eject?

And

The Why is important for me, as I need to considering the stability of these changes, and a bit of curiosity, thats all.

I guess I phrased my last comment wrongly, but the “why” = “the main culprit” = “master basically now resolves correctly the Windows’ path’s format while bundling” ( = “before master on windows was having issue with path resolving”). Does this make more sense? 🤔

hey folks, thanks for the feedback:

  • @FibreFoX here’s the issue that “collects them all” https://github.com/facebook/react-native/issues/20353 (we plan on releasing the first rc of 0.57 today, in the meantime could you test if master works for you?)
  • @shyaniv7 yes exactly, you can’t use that config because expo is still on babel6 so it’s hard to ensure that the config works for babel7

To address the various suggestions in that last comment of #19859 :

  • I have tried all three options for babel configuration - .babelrc, .babelrc.js, and babel.config.js none seem to affect the issue.
  • I’m using the transform setting (out of react-native) on macOs successfully
  • I tried copying the preprocessor file, but haven’t done any customizations yet. Unsurprisingly, it has no effect on the issue.
    • I do use one extra babel plugin (namely extensible destructuring), but I was able to get that to work on macOs without customizing the preprocessor. FibreFoX is seeing the same issue without that plugin, so it seems that should have no bearing on the problem.
    • In terms of customizing the preprocessor, since I don’t need to do so for mac, and it seems the errors are unrelated to the preprocessor, I’m not sure what changes I could make there to address the issue.

This is even reproducable when using react-native init sampleproject. My reproducable way:

react-native init sampleproject
cd sampleproject
react-native eject

Creating the file App.test.js in the sampleproject-folder having this content:

import React from 'react';
import App from './App';

import renderer from 'react-test-renderer';

it('renders without crashing', () => {
  const rendered = renderer.create(<App />).toJSON();
  expect(rendered).toMatchSnapshot();
});

Running yarn test now breaks:

PS C:\tmp\sampleproject> yarn test
yarn run v1.7.0
$ jest
 FAIL  ./App.test.js
  ● Test suite failed to run

    Cannot find module 'setupDevtools' from 'setup.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:221:17)
      at Object.<anonymous> (node_modules/react-native/jest/setup.js:33:6)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.663s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Even the provided hints/workarounds from https://github.com/facebook/react-native/issues/19859#issuecomment-407748189 did not work for me. This makes me not being able to use react-native 0.56 at all!

EDIT: the same code works flawless on latest MacOS (High Sierra) and on latest Debian Stretch. This seems to be a platform-specific bug !!!

I am too getting the same error. My setup is with TypeScript, but I don’t think it affects this. And I haven’t been able to find a working solution online. Any help would be much appreciated. System: MacOS React Native: 0.57.8 Jest: 23.6.0

@dolsem are I explained above, using jest-expo is unsafe since expo is still on babel6.

@shyaniv7 I understand your feeling about

we cannot go to production with an RC version and 0.56 fixed a major memory leak issue on android for us, so now we are stuck waiting

but remember that you can always fork and cherry pick commits that may fix it for you.

Overall, we are not releasing a new 0.57 rc because we are stuck trying to fix an issue - you can follow along here.

I tried all the suggestions above and nothing seems to work. we are using the jest-expo preset for jest and keep getting the same error

Cannot find module 'setupDevtools' from 'setup.js' in react-native/jest/setup.js 

Update: This would explain why jest-expo did not work on rn 0.56 https://blog.expo.io/expo-sdk-v29-0-0-is-now-available-f001d77fadf

regardless even with the react-native preset i cannot get it to work

@kelset yes, tried the last comment of that issue, did not work (and the author of this issue seems to have this done too).

Can you link these other windows-specific issues/pull-requests here? Would help making this more visually present.

@kelset might be, but it is very strange that this issue is only on Windows systems, but working on MacOS/Linux. The other issue was happening on MacOS so it seemed to be a different bug.