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"
]
}
}
}
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)
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(react-native)/)"
]
}
PASS __tests__/tests.js
✓ renders correctly (2303ms)
Expect the tests to pass so tests can be run for multiple platforms
https://github.com/androidian/haste-test
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
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:
"transformIgnorePatterns": [
"node_modules/?!(react-router)"
],
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.
Most helpful comment
I have also experienced this after upgrading to
jest 24.0.0withreact-native 0.55.4. Staying onjest 23.6.0prevents the issue.