【发布时间】:2020-09-21 16:15:59
【问题描述】:
我有一个非常简单的 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 会失败。我还需要做些什么才能让它发挥作用吗?
【问题讨论】: