代码拆分 `import` 会破坏 Jest 测试 [英] Code splitting `import` breaks Jest tests

查看:22
本文介绍了代码拆分 `import` 会破坏 Jest 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 webpack 的代码拆分功能,但 jest 似乎无法识别 import() 函数:

I'm using the code splitting feature of webpack, but it seems that jest doesn't recognize the import() function:

        import('myModule').then(function (myModule) {
        ^^^^^^
SyntaxError: Unexpected token import

我没有任何特殊设置.我的 npm 测试脚本只是运行 jest "test": "jest"

I don't have any special setup. My npm test script is simply run jest "test": "jest"

我怎样才能让它发挥作用?

How can I make it work?

我使用的是最新版本的 jest 20.0.4 和 babel-jest 20.0.3

I'm using the latest version of jest 20.0.4 and babel-jest 20.0.3

推荐答案

哦,我刚刚找到答案.

只需安装这个插件:https://github.com/airbnb/babel-plugin-dynamic-import-node 并将其添加到 .babelrc 文件中:

Simply install this plugin: https://github.com/airbnb/babel-plugin-dynamic-import-node and add it to the .babelrc file:

{
  ...
  "env": {
    "test": {
      "plugins": ["dynamic-import-node"]
    }
  }
}

这篇关于代码拆分 `import` 会破坏 Jest 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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