首页 文章

反应测试中意外令牌导入错误

提问于
浏览
1

我的 jest.config.js 文件包含以下给出的数据 .

但是当我运行test命令时,它给出了我的SyntaxError错误:

意外的令牌导入

当我触发测试命令时,我得到错误

e

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,
};

2 回答

  • 0

    请安装 "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,
    };
    

    如果问题仍然存在,请告诉我

  • 0

    它通常发生在Babel不处理您的测试和代码时 . Jest是一个Node.js应用程序,而Node.js不理解 import 语法 .

    我看到你定义了自己的 transform 配置 . Jest documentation说如果为 transform config选项设置一些值,它将覆盖默认值,而Jest不会使用babel-jest预处理你的代码 . 要解决此问题,您需要通过babel-jest显式定义要转换的文件:

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

相关问题