加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 为什么Jest在测试React组件时会抛出“Unexpected tok

发布时间:2020-12-15 09:30:32 所属栏目:百科 来源:网络整理
导读:我按照 Jest – React tutorial来测试React组件. 不幸的是,Jest抛出: SyntaxError: /Users/mishamoroshko/react-playground/src/search-panel/questions/__tests__/questions-test.js: /Users/mishamoroshko/react-playground/src/search-panel/questions/q
我按照 Jest – React tutorial来测试React组件.

不幸的是,Jest抛出:

SyntaxError: /Users/mishamoroshko/react-playground/src/search-panel/questions/__tests__/questions-test.js: /Users/mishamoroshko/react-playground/src/search-panel/questions/questions.js: Unexpected token ILLEGAL
at Contextify.sandbox.run (/Users/mishamoroshko/react-playground/node_modules/jest-cli/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24)
at JSDomEnvironment.runSourceText (/Users/mishamoroshko/react-playground/node_modules/jest-cli/src/JSDomEnvironment.js:108:22)
at Object.runContentWithLocalBindings (/Users/mishamoroshko/react-playground/node_modules/jest-cli/src/lib/utils.js:341:23)

重现:

> git clone git@github.com:SEEK- Jobs / react-playground.git
> cd react-playground
> npm安装
> npm测试

有任何想法吗?

更新1:

我想知道问题是Jest不知道ES6,我需要使用6to5-jest.

有没有办法在package.json中指定2个预处理器?

"jest": {
  "rootDir": "src","scriptPreprocessor": "../preprocessor.js","unmockedModulePathPatterns": [
    "../node_modules/react"
  ]
}

解决方法

确实,添加 6to5-jest解决了这个问题.

以下是我在Jest中实现多个scriptPreprocessors的方法:

// preprocessor.js

var ReactTools = require('react-tools');
var to5 = require('6to5-jest').process;

module.exports = {
  process: function(src,filename) {
    return ReactTools.transform(to5(src,filename));
  }
};

如果您有更好的方法来实现这一点,请发表评论.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读