javascript - 无法从 'react-native-implementation.js' 找到模块

标签 javascript react-native gulp jestjs

我有以下 Jest 测试:

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

关于javascript - 无法从 'react-native-implementation.js' 找到模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47269957/

相关文章:

javascript - 不一致的 JavaScript 返回值

javascript - d3.js:放大点击事件

javascript - 下拉列表不在 jQuery 中为 .slideDown() 设置动画

react-native - 如何解决react-native中的jest-haste-map错误?

javascript - Gulp:更改 css 文件的文件目标

javascript - 即使回调已完成,Gulp [4.0.2] 在异步函数完成时也会挂起

javascript - 是否可以对 Meteor 用户进行响应式(Reactive) isVerified 调用?

node.js - 安装 expo-cli : Could not resolve dependency:

javascript - 有什么方法可以声明事件对象而不是在函数的参数中?

css - 如何在从 gulp 编译 less 到 css 的同时导出多个文件?