【问题标题】:Running tests with ES6 import statements node使用 ES6 导入语句节点运行测试
【发布时间】:2021-02-05 23:47:40
【问题描述】:

我在 ubuntu 18.04.5 上使用节点 v15.0.1 和 jest 26.6.0

我已经设置了一个简单的测试用例,并且在文件的顶部我尝试使用 ES6 导入语句:

import Color from './color.js'

test("Initialized properly after construction", () => {
    expect(1 + 1).toBe(2);
});

另外,这里是 color.js 的代码:

class Color {
    constructor(r, g, b, a) {
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }
}

export {
    Color
};

当我运行 jest 时,我得到以下错误输出:


 FAIL  src/modules/color.test.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /home/daniel/Documents/raycaster/src/modules/color.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Color from './color.js'
                                                                                             ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at new Script (node:vm:100:7)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.288 s
Ran all test suites.
npm ERR! code 1

根据 Jest 文档 https://jestjs.io/docs/en/ecmascript-modules,我已将以下内容添加到我的 package.json 文件中:

"type": "module",
"jest": {
        "testEnvironment": "jest-environment-node",
        "transform": {}
}

尽管有这些配置,jest 似乎无法在 ES6 兼容模式下运行。我需要做哪些配置才能启用导入语句?

【问题讨论】:

  • 欢迎分享color.js的内容
  • @chyke007 已经添加了 color.js 的内容

标签: javascript node.js jestjs es6-modules


【解决方案1】:

我找到Node v13 / Jest / ES6 — native support for modules without babel or esm

突出显示了我需要的部分:

在我的package.json 文件中,我需要指定以下内容:

"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"

【讨论】:

    【解决方案2】:

    由于您将类作为默认导出导入,因此您需要一个默认值。要了解更多信息,请查看https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export

     class Color {
                constructor(r, g, b, a) {
                    this.r = r;
                    this.g = g;
                    this.b = b;
                    this.a = a;
                }
            }
        
            export default  Color;
    

    【讨论】:

    • 感谢您提出这个问题,一旦我在 package.json 文件中更改了用于触发 jest 的命令,我就遇到了这个问题,因此非常有帮助!
    【解决方案3】:

    正如the reference 所说,

    安装 Jest 时会自动安装 babel-jest,如果项目中存在 babel 配置,它会自动转换文件。为避免这种行为,您可以显式重置转换配置选项:

    <...>

    变换:{},

    这正是这个配置的作用,它禁用了 Babel 并防止 import 被转译。

    解决方案是删除transform: {},只故意使用transform

    提到的reference section 专用于 Node.js 中的原生 ES 模块支持。它表明transform: {} 需要启用它们:

    node --experimental-vm-modules node_modules/.bin/jest
    

    不建议经常使用,因为 Node 和 Jest 中的 ESM 支持是实验性的,可能会导致问题和缺少功能,因为 Jest 已经严重依赖 CommonJS 模块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-27
      • 2016-04-27
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 2020-08-08
      • 2020-03-21
      • 1970-01-01
      相关资源
      最近更新 更多