简体   繁体   English

在React-Native项目上开玩笑时出错

[英]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-nativemy-projectreact-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.

相关问题 在Macbook上运行react-native项目时出现路径错误 - Path error when running react-native project on my macbook 错误在React-Native中编译项目时无法执行aapt - Error Failed to execute aapt, when compiling project in React-Native 尝试安装 react-native 项目时出错 - Error when try to install react-native project 当我使用“react-native run-ios”运行 React Native 项目时显示错误 - React Native project showing error when I run it using "react-native run-ios" 使用外部插件在本机中开玩笑测试用例错误 - Jest test case error in react-native using external plugin 在对本机项目运行声纳分析时,得到与sonar.java.binaries属性相关的错误 - On running sonar analysis on react-native project getting error related to sonar.java.binaries property SyntaxError - node_modules/react-native/Libraries/polyfills/error-guard.js:缺少分号。 (14:4) 在 react native 库中运行 jest - SyntaxError - node_modules/react-native/Libraries/polyfills/error-guard.js: Missing semicolon. (14:4) on running jest in react native library 运行笑话测试进行反应时出现错误 - Plotly error when running jest test for react Jest遇到了一个带有react-native的意外令牌 - Jest encountered an unexpected token with react-native 如何在Jest和react-native中使用@providesModule - how to use @providesModule with Jest and react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM