组件正在导入包含本机模块的库。这是一个人为的例子:
import React from 'react';
import { View } from 'react-native';
import { Answers } from 'react-native-fabric';
export default function MyTouchComponent({ params }) {
return <View onPress={() => { Answers.logContentView() }} />
}
这是相关部分Answers
from react-native-fabric
:
var { NativeModules, Platform } = require('react-native');
var SMXAnswers = NativeModules.SMXAnswers;
在摩卡测试中导入此组件时,由于以下原因而失败SMXAnswers
is undefined
:
你怎么嘲笑SMXAnswers
or react-native-fabric
这样它就不会损坏并允许您测试您的组件?
p.s.:你可以看到完整设置 https://github.com/digisquare/mobile/blob/701cb8704e874864fbc13afd0325f008a5baad9e/test/setup.js和成分 https://github.com/digisquare/mobile/blob/701cb8704e874864fbc13afd0325f008a5baad9e/js/scenes/organizations/OrganizationsRow.js我正在尝试在 GitHub 上进行测试。
Use mockery
模拟任何本机模块,如下所示:
import mockery from 'mockery';
mockery.enable();
mockery.warnOnUnregistered(false);
mockery.registerMock('react-native-fabric', {
Crashlytics: {
crash: () => {},
},
});
这是一个完整的设置示例 https://github.com/digisquare/mobile/blob/f489ec8b28a91f84ebee38eda6278dc56c04fef4/test/setup.js:
import 'core-js/fn/object/values';
import 'react-native-mock/mock';
import mockery from 'mockery';
import fs from 'fs';
import path from 'path';
import register from 'babel-core/register';
mockery.enable();
mockery.warnOnUnregistered(false);
mockery.registerMock('react-native-fabric', {
Crashlytics: {
crash: () => {},
},
});
const modulesToCompile = [
'react-native',
].map((moduleName) => new RegExp(`/node_modules/${moduleName}`));
const rcPath = path.join(__dirname, '..', '.babelrc');
const source = fs.readFileSync(rcPath).toString();
const config = JSON.parse(source);
config.ignore = function(filename) {
if (!(/\/node_modules\//).test(filename)) {
return false;
} else {
const matches = modulesToCompile.filter((regex) => regex.test(filename));
const shouldIgnore = matches.length === 0;
return shouldIgnore;
}
}
register(config);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)