SyntaxError: Unexpected token { with Jest and Babel

问题描述

我在 Babel 7+ 中使用 ES6 模块。在一个函数中,我正在做这样的事情:

// some function I want to test

function something() {
...
  require('path/to/some/file.js');
...
}

那个 path/to/some/file.js 文件使用 import 这样的语句

// path/to/some/file.js

import { Something } from 'package';

当我尝试使用 Jest 运行测试时,它抱怨如下:

import { Something } from 'package';
       ^

SyntaxError: Unexpected token {

这是我在 jest.config.js 中使用的配置:

module.exports = {
  testEnvironment: 'node',};

这是来自 babel.config.js 的配置:

module.exports = {
  presets: [['@babel/preset-env',{ targets: { node: 'current' } }]],};

这些是我的开发依赖项:

"devDependencies": {
    "@babel/cli": "^7.14.5","@babel/core": "^7.14.6","@babel/node": "^7.14.7","@babel/preset-env": "^7.14.7","@types/jest": "^26.0.22","babel-jest": "^27.0.6","eslint": "^7.24.0","husky": "^6.0.0","jest": "^26.6.3","nodemon": "^2.0.7",}

我能做些什么来解决这个问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)