简体   繁体   English

React Jest测试中意外导入令牌的错误

[英]Error of unexpected token import in react jest test

My jest.config.js file contains this data given below. 我的jest.config.js文件包含以下给出的数据。

But when i am running test command it is giving me error of SyntaxError: 但是当我运行测试命令时,它给了我SyntaxError错误:

Unexpected token import 意外的令牌导入

Error i am getting when i fire test command 当我启动测试命令时出现错误

Ë

const path = require('path');

module.exports = {
bail: true,
rootDir: process.cwd(),
testRegex: '/__tests__/.*\\.test\\.jsx?$',
transform: { '/__tests__/.*': path.resolve(__dirname, 'jest.transform.js'),},
verbose: true,
};

It typically happens when your tests and code are not processed by Babel. 当您的测试和代码未被Babel处理时,通常会发生这种情况。 Jest is a Node.js application and Node.js doesn't understand import syntax. Jest是一个Node.js应用程序,而Node.js不了解import语法。

I see that you defined your own transform config. 我看到您定义了自己的transform配置。 Jest documentation says that if you set some value for transform config option it will overwrite defaults and Jest won't preprocess your code using babel-jest. Jest文档说,如果您为transform config选项设置了一些值,它将覆盖默认值,并且Jest不会使用babel-jest预处理代码。 To fix this issue you need to explicitly define what files to transform via babel-jest: 要解决此问题,您需要明确定义要通过babel-jest转换的文件:

transform: {
  '/__tests__/.*': path.resolve(__dirname, 'jest.transform.js'),
  "^.+\\.(js|jsx)$": "babel-jest",
},

Please install "babel-jest": "^23.0.1", and add the following transform config: 请安装"babel-jest": "^23.0.1",并添加以下转换配置:

const path = require('path');
module.exports = {
bail: true,
rootDir: process.cwd(),
testRegex: '/__tests__/.*\\.test\\.jsx?$',
"transform": {
  "\\.js$": "<rootDir>/node_modules/babel-jest"
},
verbose: true,
};

Let me know if the issue still persists 让我知道问题是否仍然存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM