Jest: Cannot find module 'setupDevtools' from 'setup.js' when using haste configuration option

Created on 14 Dec 2018  ·  10Comments  ·  Source: facebook/jest

🐛 Bug Report

When trying to run platform specific tests on a react-native project tests fail with
Cannot find module 'setupDevtools' from 'setup.js'
when configuring jest with the haste option.

package.json

{
  "name": "haste-test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.6.3",
    "react-native": "0.57.8"
  },
  "devDependencies": {
    "@babel/core": "7.2.0",
    "babel-core": "babel-core@^6.0.0 || ^7.0.0-0",
    "babel-jest": "23.6.0",
    "jest": "23.6.0",
    "metro-react-native-babel-preset": "0.48.5",
    "react-test-renderer": "16.6.3"
  },
  "jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
      "node_modules/(?!(react-native)/)"
    ],
    "haste": {
      "defaultPlatform": "android",
      "platforms": [
        "android",
        "ios"
      ],
      "providesModuleNodeModules": [
        "react",
        "react-native"
      ]
    }
  }
}

To Reproduce

  1. pull repo: https://github.com/androidian/haste-test
  2. yarn install
  3. yarn test
FAIL  __tests__/tests.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:3:6)
  1. Remove "haste" from jest configuration
"jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
      "node_modules/(?!(react-native)/)"
    ]
  }
  1. yarn test
 PASS  __tests__/tests.js
  ✓ renders correctly (2303ms)

Expected behavior

Expect the tests to pass so tests can be run for multiple platforms

Link to repo

https://github.com/androidian/haste-test

System Info

  System:
    OS: macOS High Sierra 10.13.6
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 9.8.0 - /usr/local/bin/node
    Yarn: 1.12.3 - ~/.yarn/bin/yarn
    npm: 5.6.0 - /usr/local/bin/npm
  npmPackages:
    jest: 23.6.0 => 23.6.0
Bug

Most helpful comment

I have also experienced this after upgrading to jest 24.0.0 with react-native 0.55.4. Staying on jest 23.6.0 prevents the issue.

All 10 comments

I have also experienced this after upgrading to jest 24.0.0 with react-native 0.55.4. Staying on jest 23.6.0 prevents the issue.

React native comes with its own haste config in its preset, so this is expected behavior - there is no merging of haste (or other) config. You need to either remove haste from your config or merge it manually

@SimenB I get this issue without any haste config in my package.json, simply when I updated jest from 23.6.0 to 24.0.0 using react-native 0.55.4.

haste is defined by react-native preset

That's an issue for RN. It's their setup file which does the jest.mock that fails

@SimenB How to fix this issue?

Ask somewhere RN devs are (stackoverflow?) - this is not an issue with Jest

Several bugfixes were needed, and I managed to get the tests running:

  • Eliminates unknown "export" token: "Unexpected token export, when import not compiled libraries"
"transformIgnorePatterns": [
    "node_modules/?!(react-router)"
],
  • Removed jsdom - no longer emulating browser environment for react native. "TypeError: Cannot set property '_eventListeners' of undefined” with jest"
  • Updated: testPathIgnorePatterns to ignore all folders except "src" so we can remove roots: ["src"] which causes: "Cannot find module 'setupDevtools' from 'setup.js'"

how can I solve this in a yarn workspace environment?

Here's how I fixed this following @SimenB's comment:

Using jest.config.js (instead of package.json)

const reactNativeJestPreset = require('react-native/jest-preset')

module.exports = {
  preset: 'react-native',
  // Override default platform to android
  haste: {
    ...reactNativeJestPreset.haste,
    defaultPlatform: 'android',
  },
}

Also be sure to run yarn jest --clearCache.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephenlautier picture stephenlautier  ·  3Comments

StephanBijzitter picture StephanBijzitter  ·  3Comments

paularmstrong picture paularmstrong  ·  3Comments

nsand picture nsand  ·  3Comments

jardakotesovec picture jardakotesovec  ·  3Comments