jest icon indicating copy to clipboard operation
jest copied to clipboard

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

Open shixukai opened this issue 10 years ago • 42 comments

 FAIL  __tests__/App-test.js
  ● Test suite failed to run

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

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:142:17)

Test Summary
 › Ran all tests.
 › 1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 0.242s)
npm ERR! Test failed.  See above for more details.

here is my config of jest

  "jest": {
    "preset": "jest-react-native",
    "preprocessorIgnorePatterns": [
      "node_modules/(?!react-native|jpush-react-native)"
    ],
    "haste": {
      "defaultPlatform": "ios",
      "platforms": [
        "ios",
        "android"
      ]
    },
    "modulePathIgnorePatterns": [
      "react",
      "react-native"
    ],
    "moduleFileExtensions": [
      "js",
      "jsx"
    ],
    "collectCoverage": false,
    "verbose": true,
    "testPathIgnorePatterns": [
      "/node_modules/",
      ".+/(resources)/.+",
      ".+/(autoMockOff.js)"
    ]
  },
  "devDependencies": {
    "babel-jest": "^15.0.0",
    "babel-plugin-module-resolver": "^2.0.0",
    "babel-polyfill": "^6.16.0",
    "babel-preset-react-native": "^1.9.0",
    "babel-preset-react-native-stage-0": "^1.0.1",
    "jest": "^15.1.1",
    "jest-react-native": "^15.0.0",
    "react-test-renderer": "^15.3.2",
    "remote-redux-devtools": "^0.4.7"
  }

any idea?

shixukai avatar Oct 02 '16 06:10 shixukai

I add "setupFiles": ["./node_modules/react-native/jestSupport/env.js"] to jest, fix it, but here comes

 FAIL  __tests__/App-test.js
  ● Test suite failed to run

    Cannot find module 'react-native-image-resizer' from 'FormCamera.js'

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:142:17)
      at Object.<anonymous> (src/components/Form/FormCamera.js:4:30)
      at Object.<anonymous> (src/components/Form/index.js:3:29)

Test Summary
 › Ran all tests.
 › 1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 1.721s)
npm ERR! Test failed.  See above for more details.

FormCamera.js

import React, { Component, PropTypes } from 'react';
import ProgressCircle from 'react-native-progress/Circle';
import ImagePreview from 'react-native-image-preview';
import ImageResizer from 'react-native-image-resizer';
...

.babelrc

{
  "presets": [ "react-native", "react-native-stage-0/decorator-support"],
  "plugins": [
    "transform-object-rest-spread",
    ["module-resolver", {
      "root": ["./"],
      "alias": {
        "src": "./src",
        "config": "./config"
      }
    }]
  ]
}

shixukai avatar Oct 02 '16 06:10 shixukai

Which version of react-native are you using?

cpojer avatar Oct 03 '16 04:10 cpojer

@cpojer 0.34.0

shixukai avatar Oct 03 '16 06:10 shixukai

Oh, remove the haste config from your Jest configuration. The jest-react-native preset brings this in automatically.

cpojer avatar Oct 03 '16 06:10 cpojer

I'm having the exact same issue. Here's my package.json:

"scripts": {
    "start": "react-native start",
    "test": "jest"
  },
  "jest": {
    "preset": "jest-react-native",
    "testPathDirs": ["./test/jest"]
  },
  "devDependencies": {
    "babel-core": "^6.14.0",
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-react": "^6.11.1",
    "babel-preset-react-native": "^1.9.0",
    "babel-preset-stage-2": "^6.13.0",
    "babel-jest": "^15.0.0",
    "babelify": "^7.3.0",
    "browserify": "^13.1.0",
    "chai": "^3.5.0",
    "enzyme": "^2.4.1",
    "gsap": "^1.19.0",
    "gsap-react-plugin": "^1.0.2",
    "gulp": "^3.9.1",
    "gulp-browserify": "^0.5.1",
    "gulp-minify": "0.0.14",
    "gulp-sourcemap": "^1.0.1",
    "gulp-sourcemaps": "^1.6.0",
    "gulp-uglify": "^2.0.0",
    "gulp-watchify": "^0.6.0",
    "jasmine": "^2.4.1",
    "jasmine-jquery": "^2.1.1",
    "jsdoc": "^3.4.0",
    "karma": "^1.3.0",
    "karma-babel-preprocessor": "^6.0.1",
    "karma-browserify": "^5.1.0",
    "karma-chrome-launcher": "^1.0.1",
    "karma-firefox-launcher": "^1.0.0",
    "karma-html-reporter": "^0.2.7",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.0",
    "karma-jasmine-jquery": "^0.1.1",
    "karma-nyan-reporter": "^0.2.4",
    "karma-phantomjs-launcher": "^1.0.1",
    "karma-safari-launcher": "^1.0.0",
    "mocha": "^3.0.2",
    "pump": "^1.0.1",
    "radium": "^0.18.1",
    "react": "^15.3.2",
    "react-dom": "^15.3.1",
    "react-native-accordion": "^1.0.1",
    "react-native-collapsible": "^0.7.0",
    "react-native-mock": "^0.2.6",
    "sinon": "^1.17.6",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0",
    "watchify": "^3.7.0",
    "jest": "16.0.1",
    "jest-react-native": "^15.0.0",
    "react-test-renderer": "^15.3.2"
  }

Any idea why it's not working?

misaizdaleka avatar Oct 07 '16 11:10 misaizdaleka

you don't seem to have react-native as a dependency of your project.

cpojer avatar Oct 07 '16 12:10 cpojer

I have it, but not as dev dependency, just as regular dependency, samo as @shixukai.

misaizdaleka avatar Oct 07 '16 17:10 misaizdaleka

I did some more research and if I put my test in __ tests __ folder, everything works fine. However, when I move them to /test/jest, and add this param:

"testPathDirs": ["./test/jest"]

Then it starts failing with ReactNativeDefaultInjection error.

misaizdaleka avatar Oct 09 '16 19:10 misaizdaleka

This issue definitely seems to be caused by setting the testPathDirs config. Instead, you may want to try replacing it with testPathIgnorePatterns to get around the issue for now. It worked for me, but it's obviously not ideal.

For example, change "testPathDirs": ["src"] to "testPathIgnorePatterns": ["/node_modules/", "/example/", "/lib/"]

cooperka avatar Oct 27 '16 00:10 cooperka

That's actually correct, testPathDirs is the problem.

misaizdaleka avatar Oct 27 '16 10:10 misaizdaleka

Yes, testPathDirs is actually a different name for "roots". We should really rename this and fix it. Jest only looks in folders that are called "testPathDirs" and it doesn't look anywhere else.

cpojer avatar Oct 28 '16 03:10 cpojer

I am running into this issue also on this repo, however I have not defined testPathDirs or anything other than "preset": "jest-react-native".

This project doesn't have an index.ios.js or index.android.js since it's not an actual React Native app, but I can't imagine that's what's causing the issue.

I've written basic tests based on the example here and have named them with the ComponentName.test.js convention (the files are Button.test.js and TextInput.test.js). They show up in the test results, but they fail with the same error.

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

Is there anything else which might cause this?

jasonmerino avatar Nov 03 '16 15:11 jasonmerino

@jasonmerino try jest@test

cpojer avatar Nov 03 '16 15:11 cpojer

When I npm i jest@test and npm test with that new version of jest I now get this message.

● The settings `scriptPreprocessor` and `preprocessorIgnorePatterns` were replaced by `transform` and `transformIgnorePatterns` which support multiple preprocessors.

  Jest now treats your current settings as:

    "transform": {".*": "undefined"}
    "transformIgnorePatterns": "node_modules/(?!(jest-)?react-native|react-clone-referenced-element)"

  Please update your configuration.

  Jest changed the default configuration for tests.

  Configuration Documentation: https://facebook.github.io/jest/docs/configuration.html
  Jest Issue Tracker: https://github.com/facebook/jest/issues

Which is strange since the only configuration I have in the package.json is the preset. Could there be another piece of config hiding elsewhere in the project?

jasonmerino avatar Nov 03 '16 17:11 jasonmerino

You also need jest-react-native@test

cpojer avatar Nov 03 '16 18:11 cpojer

Okay, I did npm i jest-react-native@test and npm test again and I'm back to the original error I had (Cannot find module 'ReactNativeDefaultInjection' from 'setup.js') without the previous warning message about jest settings.

Here's the output from my npm i

npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
[email protected] /Users/jason.merino/Code/react-native-splat
├── UNMET PEER DEPENDENCY [email protected]
├── [email protected]  extraneous
└── [email protected]  extraneous

npm WARN [email protected] requires a peer of eslint-plugin-import@^1.16.0 but none was installed.
npm WARN [email protected] requires a peer of eslint-plugin-import@^1.16.0 but none was installed.

So it looks like I'm getting the test versions of jest and jest-react-native.

jasonmerino avatar Nov 03 '16 18:11 jasonmerino

+1 I have the same issue.

shahen94 avatar Nov 13 '16 14:11 shahen94

Oh man, i figured this out. We need to install react as dependency or devDependency. 🎉 Cheers !

shahen94 avatar Nov 13 '16 14:11 shahen94

We had this issue to using [email protected], in our case was not installing the right version of react for react-native

We had this in our package.json

{
    "react": "^15.3.2",
    "react-native": "^0.37.0",
}

and doing a npm install gave us this output

[email protected] requires a peer of react@~15.3.1 but none was installed.

This was due react@^15.3.2 resolve to [email protected] due how npm semver works.

Setting the react version to react@~15.3.2 solved the issue. Change your package.json to look like this

{
    "react": "~15.3.2",
    "react-native": "^0.37.0",
}

orlando avatar Nov 17 '16 02:11 orlando

even { "react": "^15.4.0", "react-native": "^0.37.0", } it doesnt work .

tianwawawa avatar Nov 22 '16 05:11 tianwawawa