[英]Error when running jest on React-Native project
When running test suites on my react-native project the following errors keep appearing. 在我的本机项目上运行测试套件时,会继续出现以下错误。
/.../node_modules/@expo/react-native-action-sheet/ActionSheet.ios.js:3
import React from 'react';
^^^^^
SyntaxError: Unexpected identifier
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (node_modules/@expo/react-native-action-sheet/index.js:1:45)
The following is the test code used. 以下是使用的测试代码。 It is a simple snapshot test in Jest
这是Jest中的简单快照测试
//ContactList Tests
import React from 'react';
import renderer from 'react-test-renderer';
import ContactList from '../components/ContactList';
//ContactList Snapshot
test('renders correctly', () => {
const tree = renderer.create(<ContactList />).toJSON();
expect(tree).toMatchSnapshot();
});
I am running react-native version 0.55.4 and Jest version 23.6.0 我正在运行react-native版本0.55.4和Jest版本23.6.0
I've seen this before 我以前看过
From Jest docs : 来自Jest docs :
Sometimes it happens (especially in React Native or TypeScript projects) that 3rd party modules are published as untranspiled.
有时会发生(尤其是在React Native或TypeScript项目中)第三方模块以未编译的形式发布。 Since all files inside node_modules are not transformed by default, Jest will not understand the code in these modules, resulting in syntax errors.
由于默认情况下不会转换node_modules内部的所有文件,因此Jest无法理解这些模块中的代码,从而导致语法错误。 To overcome this, you may use transformIgnorePatterns to whitelist such modules.
为了克服这个问题,您可以使用transformIgnorePatterns将此类模块列入白名单。 You'll find a good example of this use case in React Native Guide.
您可以在《 React Native Guide》中找到一个很好的例子。
In the React Native Guide they mention, you'll find the following section from the package.json (this would be nested inside a jest
object): 他们提到的《 React Native Guide》中,您将从package.json中找到以下部分(该部分将嵌套在一个
jest
对象中):
"transformIgnorePatterns": [
"node_modules/(?!(react-native|my-project|react-native-button)/)"
]
This is a RegExp with a negative lookahead, so it's saying: inside the node_modules
, anything that isn't immediately followed by react-native
, my-project
, or react-native-button
is to be ignored (not transformed). 这是一个带有负前瞻性的RegExp,也就是说:在
node_modules
,任何不立即跟在react-native
, my-project
或react-native-button
都将被忽略(不转换)。
Or to simplify this double-negative, it is going to transform those three packages inside of node_modules
, but no others. 或简化这双负, 它要这三个包变换的内部
node_modules
,但没有其他人。
In your case, this regexp might look something like this: 在您的情况下,此正则表达式可能如下所示:
"node_modules/(?!(react-native|@expo/react-native-action-sheet)/)"
This would allow both react-native
and @expo/react-native-action-sheet
to be transpiled (I think...you might have to play around with it a little. There may be some character escapes or some other packages that need to be white-listed). 这将允许将
react-native
和@expo/react-native-action-sheet
都进行编译(我认为...您可能需要对其进行一些操作。可能会有一些字符转义或一些其他需要的软件包列入白名单)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.