【问题标题】:How to use ES modules in Jest [duplicate]如何在 Jest 中使用 ES 模块 [重复]
【发布时间】:2021-10-07 01:30:21
【问题描述】:

app.js

export const temp = "Hello World";

app.test.js

import { temp } from "./app.js";

当我运行 npm test 时,它会抛出 “SyntaxError: Cannot use import statement outside a module”

这是test 脚本:{scripts: {test: "jest"}}

我通过在package.json 中添加"type": "module" 来启用ES 模块。

【问题讨论】:

    标签: javascript testing jestjs


    【解决方案1】:

    我必须添加 experimental-vm-modules 标志

    package.json文件:

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

    【讨论】:

      【解决方案2】:

      确保您遵循 Jest 文档的 the ECMA Modules page 中提供的说明。如果这不起作用,你能提供一个复制品吗?了解您的 Node/Jest/etc 版本也会有所帮助。

      【讨论】:

        猜你喜欢
        • 2021-05-15
        • 1970-01-01
        • 2018-01-26
        • 2018-12-05
        • 2021-10-01
        • 2021-10-21
        • 2022-12-11
        • 1970-01-01
        • 2021-12-02
        相关资源
        最近更新 更多