reactjs - 开 Jest - react native 语法错误 : Unexpected token import

标签 reactjs react-native jestjs

我正在 native react 中运行 Jest 测试,但它失败并给出“SyntaxError:意外的 token 导入”

这是我的修剪器导入代码:-

import React, { Component } from 'react';
import { Container,
  Content,
  Text,
  Button,
  Icon,
  Footer,
  FooterTab,
  Item,
  Input,
  View,
  Picker
} from 'native-base';

import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

这是错误:

失败测试/home-page-test.js ● 测试套件运行失败

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://facebook.github.io/jest/docs/en/configuration.html

Details:

/Users/myuser/sampleApp/node_modules/native-base-shoutem-theme/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import connectStyle from "./src/connectStyle";
                                                                                         ^^^^^^

SyntaxError: Unexpected token import

  1 | import React, { Component } from 'react';
> 2 | import {
    | ^
  3 |   Header,
  4 |   Title,
  5 |   Button,

不确定我在导入时做错了什么。非常欢迎任何提示。

最佳答案

我知道已经晚了,但对于仍在寻找解决方案的人来说 here描述了如何解决这个问题

只需将此代码段添加到您的 Jest 配置文件中即可:

"jest": {
    "preset": "react-native",
    "transformIgnorePatterns": [
      "node_modules/(?!react-native|native-base-shoutem-theme|@shoutem/animation|@shoutem/ui|tcomb-form-native)"
    ]
  },

关于reactjs - 开 Jest - react native 语法错误 : Unexpected token import,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50882379/

相关文章:

javascript - 根据登录状态使用 React-Native-Router-Flux 显示不同的组件

javascript - 在 React Native 中对应用程序使用 Base64 图像字符串会导致崩溃

javascript - 带有反应导航的 Redux (reactnavigation.org)

javascript - 如何在 enzyme 中渲染假 DOM 中的组件?

javascript - 如何模拟内部函数?

javascript - 在导航中 react native 多颜色标题

ios - 样式表违规可按下错误 : "pointerEvents" is not a valid style property

mocking - 如何使用 Jest 模拟对象中的特定函数?

javascript - 设置间隔不更新变量

jestjs - Jest --watch 始终运行所有测试