node.js - 使用 Jest 运行测试时无法使用 uuid 模块

标签 node.js jestjs node-uuid

我有一个非常简单的 Node.js ( 12.16.3 ) 应用程序,它使用 Express 4.17.1 .我正在尝试使用 Jest 26.0.1运行测试套件,但由于 uuid 的某些问题而同样失败整个项目使用的模块(版本 8.1.0):

[x80486@uplink:~/Workshop/node-guacamole]$ npm run test 

> node-guacamole@0.3.0 test /home/x80486/Workshop/node-guacamole
> node --experimental-modules --experimental-vm-modules ./node_modules/.bin/jest --coverage --detectOpenHandles --forceExit --verbose

(node:71155) ExperimentalWarning: The ESM module loader is experimental.
 FAIL  src/domain/customer.test.js
  ● Test suite failed to run

    SyntaxError: The requested module 'uuid' does not provide an export named 'v4'

      at jasmine2 (node_modules/jest-jasmine2/build/index.js:228:5)

 FAIL  src/service/customer.service.test.js
  ● Test suite failed to run

    SyntaxError: The requested module 'uuid' does not provide an export named 'v4'

          at async Promise.all (index 4)
      at jasmine2 (node_modules/jest-jasmine2/build/index.js:228:5)

 FAIL  src/handler/customer.handler.test.js
  ● Test suite failed to run

    SyntaxError: The requested module 'uuid' does not provide an export named 'v4'

          at async Promise.all (index 2)
          at async Promise.all (index 7)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 3 failed, 3 total
Tests:       0 total
Snapshots:   0 total
Time:        0.63 s
Ran all test suites.

我正在导入如下模块:import { v4 } from "uuid" ;另一方面,应用程序成功运行:

[x80486@uplink:~/Workshop/node-guacamole]$ npm run start:dev 

> node-guacamole@0.3.0 start:dev /home/x80486/Workshop/node-guacamole
> nodemon --experimental-modules --experimental-vm-modules ./src/main.js

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node --experimental-modules --experimental-vm-modules ./src/main.js`
(node:74672) ExperimentalWarning: The ESM module loader is experimental.
2020-06-03T03:28:48.889Z [debug] - Server running at http://localhost:8080
2020-06-03T03:28:48.889Z [info] - Press CTRL-C to stop

...一切正常。我很困惑......我不明白为什么只有 Jest 会失败。我还需要做些什么才能使其正常工作吗?

最佳答案

TL;DR: Jest 还不支持 "exports" 字段在 package.json .

问题是 Node.js 使用 ESM 版本,因为它理解 package.json 中的“exports”字段,但由于 Jest 还不支持它,Jest 使用 package.json 中的“main”字段导出 CommonJS 版本.见 the relevant package.json section :

...
"main": "./dist/index.js",
"exports": {
  "./package.json": "./package.json",
  ".": {
    "require": "./dist/index.js",
    "import": "./wrapper.mjs"
  }
},
...

这是做什么的:
  • 默认导出为 main , 一如既往。
  • 如果 exports可以理解,这会覆盖 "main"导出
  • 默认导出 "."定义了一个 require 和 import,所以 Node.js 使用“import”
  • 但问题是 the Jest issue表示它还不理解“包导出”,因此 Jest 正在尝试加载 CommonJS 文件(从“main”),而 Node.js 加载 ESM 文件。

  • found the exact same issue and documented it here .作为一个实验,这应该适用于 Jest:
    import uuid from 'uuid';
    const { v4 } = uuid;
    

    但这不适用于 Node.js since UUID does not define a default export .

    你有两个现实的选择:
  • 等到 Jest 支持包导出,他们在 ESM 支持方面做得很好,我认为不会花很长时间。
  • Upvote my proposaluuid他们还导出默认的包,例如 export default uuidwrapper.mjs ,这将允许您使用我在上面放置的代码段。
  • 关于node.js - 使用 Jest 运行测试时无法使用 uuid 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62164814/

    相关文章:

    node.js - 在 Javascript 中生成 UUID 数组

    node.js - 查找 View 失败

    node.js - 'typeof Sequelize' 类型缺少 'Sequelize' : Sequelize, 配置、modelManager、connectionManager 等 23 个类型中的以下属性

    javascript - 使用 jest 测试 Javascript 对 api 的调用

    reactjs - 使用react-testing-library在表单中提交时如何测试已调用的函数?

    node.js - 在 Node 中使用 128 位长(uuid)还是 7 位长(shortid)唯一 ID 生成器更好?

    javascript - 如何使用 Nestjs 和 Multer 读取上传的文件 (text/.csv)

    node.js - 从 AWS lambda 函数写入 dynamodb

    jestjs - 运行 jest-puppeteer 测试时未处理的错误