使用React Native 0.61.0从'setupJest.js'中找不到模块'EventEmitter'

时间:2019-09-24 22:20:44

标签: react-native jestjs

在运行React Native测试之前,我拥有以下class Module(models.Model): STATUS_TYPES = ( ('Active', 'Active'), ('Inactive', 'Inactive'), ) topic = models.CharField(max_length = 200) teacher_name = models.CharField(max_length = 100) status = models.CharField(max_length=30, default='Active', choices=STATUS_TYPES) subject = models.ForeignKey(Subject, on_delete=models.CASCADE) created_dt = models.DateTimeField(default=datetime.now) def __str__(self): return self.topic class Meta: db_table = 'modules' class Session(models.Model): grade_level = models.CharField(max_length = 30) num_students = models.IntegerField(default=0) session_dt = models.DateTimeField(default=datetime.now) subject = models.ForeignKey(Subject, on_delete=models.CASCADE) module = models.ForeignKey(Module, on_delete=models.CASCADE) school = models.ForeignKey(School, on_delete=models.CASCADE) def __str__(self): return self.school.school_name + ' on ' + self.session_dt class Meta: db_table = 'sessions' 文件。

setupJest.js

这是一个示例测试:

// Required to correctly polyfill React-Native

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

global.XMLHttpRequest = jest.fn();
global.fetch = jest.fn();

if (typeof window !== 'object') {
  global.window = global;
  global.window.navigator = {};
}

const EventEmitter = require('EventEmitter');
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');

class NativeEventEmitter extends EventEmitter {
  constructor() {
    super(RCTDeviceEventEmitter.sharedSubscriber);
  }
}

这在React Native 0.60.x上运行良好。但是,使用React Native 0.61.0时,出现以下错误:

/**
 * @format
 */

import 'react-native';
import React from 'react';
import App from '../App';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

jest.mock('NativeEventEmitter');

it('renders correctly', () => {
  renderer.create(<App />);
});

如果我更改导入以匹配Cannot find module 'EventEmitter' from 'setupJest.js' 中文件的位置,那么我将走得更远。

node_modules

有什么想法如何更新我的代码,使其能够与React Native 0.61.0一起使用?

const EventEmitter = require('react-native/Libraries/vendor/emitter/EventEmitter');
const RCTDeviceEventEmitter = require('react-native/Libraries/EventEmitter/RCTDeviceEventEmitter');

您可以通过克隆以下存储库并运行其测试来重现此问题:

Cannot find module 'NativeEventEmitter' from 'App-test.js'

  10 | import renderer from 'react-test-renderer';
  11 | 
> 12 | jest.mock('NativeEventEmitter');
     | ^
  13 | 
  14 | it('renders correctly', () => {
  15 |   renderer.create(<App />);

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
  at Object.<anonymous> (tests/App-test.js:12:1)

1 个答案:

答案 0 :(得分:1)

RN库具有NativeEventEmitter类的模拟实现。只需从setupJest.js中删除以下代码:

const EventEmitter = require('EventEmitter');
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');

class NativeEventEmitter extends EventEmitter {
  constructor() {
    super(RCTDeviceEventEmitter.sharedSubscriber);
  }
}

在测试文件中设置模拟:

jest.mock(
  '../node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter',
);

别忘了删除:

jest.mock('NativeEventEmitter');