带有开玩笑的错误导入:意外的令牌导入 [英] import with jest error: Unexpected token import

查看:81
本文介绍了带有开玩笑的错误导入:意外的令牌导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到类似的问题,但仍然找不到可行的解决方案.

I've seen similar questions but still can't find a viable solution.

我正在尝试将Jest集成到一个正在运行的项目中,该项目在数百个地方使用导入/导出默认值.以下测试确实适用于使用require的Jest:

I'm trying to integrate Jest into a working project, which uses import/export default in hundreds of places. The following test does work for Jest using require:

const bar = require('../../flows/foo');

test('adds 1 + 2 to equal 3', () => {
  expect(bar.foobar(1, 2)).toBe(3);
});

当出口是:

module.exports = { 
 foobar: foobar,
  fizz: fizz
}

我要测试的功能可以通过以下方式导出:

The functions I'll want to be testing however are exported using:

export default {
  foobar: foobar,
  fizz: fizz
};

因此,当我尝试更新要导入的测试时:

So when I try to update my test to import:

import foobar from '../../flows/foo';

带有导出:

export default {foobar: foobar};

我得到了错误

SyntaxError: Unexpected token import

推荐答案

所有步骤:

// run this command (or npm equivalent)
yarn add @babel/core @babel/preset-env

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

Jest会自动将其拾取,不需要其他配置.

Jest automatically picks it up, no other configuration required.

这篇关于带有开玩笑的错误导入:意外的令牌导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆