我有一个非常简单的 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”我 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 .
你有两个现实的选择:
uuid
他们还导出默认的包,例如 export default uuid
在 wrapper.mjs
,这将允许您使用我在上面放置的代码段。 关于node.js - 使用 Jest 运行测试时无法使用 uuid 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62164814/