【问题标题】:How to use ESM tests with jest?如何将 ESM 测试与 jest 一起使用?
【发布时间】:2021-10-27 14:00:51
【问题描述】:

我的设置

  • 节点 v14.16.0
  • 我有一个测试文件,其中包含一个使用 ESM 模块 (import) 编写的简单测试。
  • package.json我有一个脚本(遵循Jest documentation适应Windows):"test": "set \"NODE_OPTIONS=--experimental-vm-modules\" && npx jest"
  • 我的测试使用npm run test(上面的脚本)运行

我的目标:使用 jest 运行 ESM 测试。

我的尝试:

(1) 带有 .js 测试的 CommonJS 应用程序

  • package.json 包含 "type": "commonjs"
  • 测试文件名为test.js
  • 错误:SyntaxError: Cannot use import statement outside a module(这是预期的,因为测试是作为 commonjs 文件运行的。)

(2) 带有 .mjs 测试的 CommonJS 应用程序

  • package.json 包含 "type": "commonjs"
  • 测试文件名为test.mjs
  • 错误:No tests found(Jest 没有找到文件?)

(3) 带 .js 或 .mjs 的 ESM 应用程序

  • package.json 现在有 "type": "module"
  • 名为test.mjstest.js 的测试文件
  • 错误:ReferenceError: module is not defined at ...jest.config.js:6:1

(4) CommonJS App和CommonJS测试

  • package.json 包含 "type": "commonjs"
  • 测试文件名为test.js,但使用commonjs require 语法(并且没有我想要包含的ESM 模块);请注意,这不是我想要的,只是为了表明测试本身没有问题。
  • 测试运行良好并通过

【问题讨论】:

  • 你提到你的测试文件是一个ES模块,但是你的测试文件呢?另外,如果您似乎真正需要的是 ES 模块测试,为什么还要提到您使用测试文件作为 Common JS 模块(在 #4 中)?
  • 您是否尝试过使用cross-env 进行任何尝试?请注意 Jest 文档第二步中关于使用您链接到的 ESM 的评论:“在 Windows 上,您可以使用 cross-env 来设置环境变量。”
  • @OfirD -- 我的测试是超级简单的占位符,甚至还没有连接到测试文件,并且没有任何错误表明这类事情是一个问题。对于#4,我解释它只是为了表明测试文件没有问题。
  • @Morgan -- 我没有尝试cross-env,因为这个项目的所有开发人员都在 Windows 机器上,我希望少一点依赖。 set 适用于 Windows (docs.microsoft.com/en-us/windows-server/administration/…)
  • @Luke 我听说你少了一个依赖。我之所以提出它,是因为我提供的答案没有使用NODE_OPTIONS

标签: javascript node.js jestjs node-modules


【解决方案1】:

对我来说,只需将脚本“测试”设置如下:

"test": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest"

是的,别忘了 -> npm i cross-env jest

【讨论】:

    【解决方案2】:

    经过大量的摆弄,这是我成功的设置(重要的部分):

    • package.json
      "type": "commonjs",
      "script": {
        "test": "NODE_OPTIONS=--experimental-vm-modules jest"
      }
    
    • jest.config.mjs(是的,esModule)
    export default {
      moduleFileExtensions: [
        "mjs",
        // must include "js" to pass validation https://github.com/facebook/jest/issues/12116
        "js",
      ],
      testRegex: `test\.mjs$`,
    };
    

    必须包含 "js" 是时代模块的历史遗物,其中显然是 .js 文件。关注https://github.com/facebook/jest/issues/12116 获取更新。

    【讨论】:

      【解决方案3】:

      以下是我使用 ESM 通过测试运行 Jest 所采取的步骤。测试中的源文件也是使用 ESM 编写的。

      1. 将我的节点版本设置为 14.16.0
      2. npm i jest -D 将 Jest 安装为开发依赖项
      3. "type": "module" 添加到package.json
      4. package.json 中更新scripts 以包含"test": "node --experimental-vm-modules ./node_modules/.bin/jest"
      5. 创建一个jest.config.js文件,内容如下:export default { transform: {} }
      6. 至少有一个可以使用默认testMatch 找到的测试(我的在__tests__ 目录中)
      7. npm test

      在 GitHub 上的webpack-strip-debug-loader 存储库中有一个更完整的示例。

      【讨论】:

      • 这似乎与我的尝试 #3 相同,但由于某种原因,这现在对我有用。我不确定默认配置是否需要您的第 5 步。我还切换到.mjs 用于测试文件,因此添加到我的jest.config.mjs 文件中:"mjs"moduleFileExtensions,以及"**/?(*.)+(spec|test).(m)js?(x)"testMatch
      • 很高兴你让它工作。我添加并使用了第 5 步,因为它直接来自 jest documentation on using esm(项目符号 #1)。注意默认transform使用babel-jest
      • Jest 27.1.1 包括对模拟 ESM 导入的支持:github.com/facebook/jest/releases/tag/v27.1.1
      • @Luke 你知道了吗?看来您不能拥有"type": "commonjs" 并将测试编写为.mjs
      猜你喜欢
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      • 2021-09-17
      相关资源
      最近更新 更多