找不到'react-native-implementation.js'模块



我有以下玩笑测试:

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();
});

我调用的IndexSign组件调用此StyleSheet组件:

import {StyleSheet} from 'react-native';
export default StyleSheet.create({
  //some styles
});

为了测试,我正在使用 Gulp:

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

问题是,当我运行此测试时,我得到:

● 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)

知道为什么会这样吗?

为什么它在react-native-implementation.js中搜索StyleSheet而不是我导入的react-native

为什么找不到StyleSheet

我遇到了同样的问题。添加

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

package.json为我修复了错误。

如果您保留单独的配置文件,例如jest.config.js.在其中添加预设。签出示例配置文件

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/'
}

有两种方法可以解决此问题。
1.确保根文件夹中没有或删除任何名为jest.config.js的文件。
2.如果要自定义jest.config.js文件,请确保其中有一个预设节点。
这样:

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

我遇到了同样的问题。现在它已解决。

我的包.json看起来像这样:

{
  "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"
      ]
    }
  }

我使用的是 react-native 0.59.5,我没有手动重置 .babelrc 或 jest.config.js我只是安装了依赖项。安装完成后,它会为我自动生成预设。我的 babel.config.js 看起来像:

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

我按照此链接中的说明进行操作

相关内容

  • 没有找到相关文章