最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot find module from 'react-native-implementation.js' - Stack Overflow

programmeradmin3浏览0评论

I have the following Jest test:

import React from 'react';
import IndexSign from '../IndexSign';
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  const tree = renderer.create(
    <IndexSign index={1}/>
  ).toJSON();
  expect(tree).toMatchSnapshot();
});

The IndexSign component that I am calling calls this StyleSheet component:

import {StyleSheet} from 'react-native';

export default StyleSheet.create({
  //some styles
});

For testing, I am using Gulp:

gulp.task('tests', () => {
    process.env.NODE_ENV = 'test';
    return gulp.src('src').pipe(jest({
    }));
});

The problem is that when I run this test, I get:

● Test suite failed to run

Cannot find module 'StyleSheet' from 'react-native-implementation.js'

  at Resolver.resolveModule (../node_modules/jest-resolve/build/index.js:142:17)
  at Object.StyleSheet (../node_modules/react-native/Libraries/react-native/react-native-implementation.js:98:25)
  at Object.<anonymous> (styles/Styles.js:5:13)

Any idea why this is happening?

Why is it searching for StyleSheet in react-native-implementation.js rather than react-native, which I imported?

And why can it not find StyleSheet?

I have the following Jest test:

import React from 'react';
import IndexSign from '../IndexSign';
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  const tree = renderer.create(
    <IndexSign index={1}/>
  ).toJSON();
  expect(tree).toMatchSnapshot();
});

The IndexSign component that I am calling calls this StyleSheet component:

import {StyleSheet} from 'react-native';

export default StyleSheet.create({
  //some styles
});

For testing, I am using Gulp:

gulp.task('tests', () => {
    process.env.NODE_ENV = 'test';
    return gulp.src('src').pipe(jest({
    }));
});

The problem is that when I run this test, I get:

● Test suite failed to run

Cannot find module 'StyleSheet' from 'react-native-implementation.js'

  at Resolver.resolveModule (../node_modules/jest-resolve/build/index.js:142:17)
  at Object.StyleSheet (../node_modules/react-native/Libraries/react-native/react-native-implementation.js:98:25)
  at Object.<anonymous> (styles/Styles.js:5:13)

Any idea why this is happening?

Why is it searching for StyleSheet in react-native-implementation.js rather than react-native, which I imported?

And why can it not find StyleSheet?

Share Improve this question asked Nov 13, 2017 at 17:17 bskybsky 20.2k54 gold badges166 silver badges282 bronze badges 1
  • 1 Have you found a solution? Stumbled upon this as well – adrienharnay Commented Dec 10, 2017 at 19:55
Add a comment  | 

3 Answers 3

Reset to default 17

I ran into the same issue. Adding

"jest": {
    "preset": "react-native"
},

in the package.json fixed the error for me.

Incase you are keeping separate config file like jest.config.js. Add preset in it. Checkout the sample config file

module.exports = {
  preset: 'react-native',
  setupFiles: ['<rootDir>/__test__/setup.js'],
  moduleNameMapper: {
    '\\.(css|less)$': 'identity-obj-proxy',
    '^.+\\.(jpg|jpeg|gif|png|mp4|mkv|avi|webm|swf|wav|mid)$': 'jest-static-stubs/$1'
  },
  globals: {
    __DEV__: true
  },
  collectCoverageFrom: [
    '**/src/**/*.{js,jsx}',
    '!**/src/**/style.js',
    '!**/src/**/index.js',
    '!**/src/theme/**',
    '!**/android/**',
    '!**/ios/**',
    '!**/node_modules/**',
    '!**/scripts/**',
    '!**/__test__/**'
  ],
  verbose: true,
  testPathIgnorePatterns: ['/node_modules/'],
  testResultsProcessor: 'jest-sonar-reporter',
  testURL: 'http://localhost/'
}

There are two ways of fixing this issue.
1. Make sure you don't have or delete any file called jest.config.js in your root folder.
2. If you want to have a custom jest.config.js file, make sure you have a preset node in it.
Like so:

 module.exports = {
    preset: "react-native",
    verbose: true,
  };

I was facing the same problem. Now it is resolved.

My package.json looks like this :

{
  "name": "ReactNativeTDDStarterKit",
  "version": "0.0.1",
  "private": true,
  "scripts": {
  "start": "node node_modules/react-native/local-cli/cli.js start",
     "test": "jest"
   },
  "dependencies": {
     "react": "16.8.3",
     "react-native": "0.59.5"
   },
   "devDependencies": {
     "@babel/core": "7.4.4",
     "@babel/runtime": "7.4.4",
     "babel-jest": "24.8.0",
     "babel-preset-flow": "^6.23.0",
     "babel-preset-react-native": "4.0.0",
     "enzyme": "^3.3.0",
     "enzyme-adapter-react-16": "^1.1.1",
     "enzyme-to-json": "^3.3.1",
     "jest": "24.8.0",
     "metro-react-native-babel-preset": "0.54.0",
     "react-dom": "^16.8.6",
     "react-test-renderer": "16.8.3"
    },
    "jest": {
      "preset": "react-native",
      "snapshotSerializers": [
        "enzyme-to-json/serializer"
      ],
      "setupFiles": [
        "<rootDir>/jest/setup.js"
      ]
    }
  }

I am using react-native 0.59.5, I did not reset .babelrc or jest.config.js manually, I just installed the dependencies. It automatically generated presets for me after the installation is completed. My babel.config.js looks like :

module.exports = {
   presets: ['module:metro-react-native-babel-preset'],
};

I followed instructions from this Link

发布评论

评论列表(0)

  1. 暂无评论