Cannot find module from 'react

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 ran into the same issue. Adding

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

in the package.json fixed the error for me.

链接地址: http://www.djcxy.com/p/40894.html

上一篇: Python中的元类是什么?

下一篇: 从'反应中找不到模块