我在尝试运行测试用例代码时遇到错误。我正在使用 react native with jest 。在升级 0.40 之前一切正常。现在是 0.42,我所有的测试用例都停止工作并在错误之后出现错误。
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React, { Component, Children, PropTypes } from 'react';
^^^^^^
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
at Object.<anonymous> (node_modules/react-native-root-siblings/lib/AppRegistryInjection.js:3:22)
at Object.<anonymous> (node_modules/react-native-root-siblings/lib/SiblingsManager.js:3:27)
这是我的.babelrc代码
{
"presets": [
"react-native"
],
"plugins": [
"transform-decorators-legacy"
]
}
我没有得到什么问题。
最佳答案
我遇到了与 0.42 完全相同的问题。我一直在敲脑袋,直到找到一个解决方案。
你需要在package.json中写ignores。我的例子:
"jest": {
"preset": "react-native",
"setupFiles": [
"<rootDir>/src/config/jest.js"
],
"transformIgnorePatterns": [
"<rootDir>/(node_modules)/(?!react-native|react-navigation|bugsnag-react-native)"
],
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/babel-jest"
}
我的 /config/jest.js 看起来像:
jest.mock('Linking', () => ({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
openURL: jest.fn(),
canOpenURL: jest.fn(),
getInitialURL: jest.fn().mockImplementation(() => new Promise((resolve) => resolve()))
}));
jest.mock('mobx-react/native', () => require('mobx-react/custom'));
jest.mock('react-native-mixpanel', () => ({
sharedInstanceWithToken: jest.fn(),
trackWithProperties: jest.fn()
}));
jest.mock('bugsnag-react-native', () => ({
Client: jest.fn(),
Configuration: jest.fn()
}));
我不保证这会直接解决您的所有问题。但是,我们的想法是忽略所有“作恶者”(在您的情况下为 react-native-root-siblings),从而避免此类错误消息。
关于javascript - 与 React Native 开 Jest 得到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43137058/