mocha 测试 中使用 async 出现错误
describe("#textbook.js", async () => {
describe("#searchTextBook", async () => {
const res = null;
const isbn = "978-7-107-29068-8";
it('响应代码必须为 0', async () => {
await searchTextBook(isbn);
});
});
});
$ mocha --require @babel/register --recursive
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/lee/Documents/work/item-bank-system/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js
require() of ES modules is not supported.
require() of /home/lee/Documents/work/item-bank-system/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js from /home/lee/Documents/work/item-bank-system/test/apis/kb/textbook.spec.js is an ES module file as it is a .js file who
se nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename asyncToGenerator.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/lee/Documents/work/item-bank-system/node_modules/@babel/runtime/helpers/esm/package.json.
at Module._extensions..js (internal/modules/cjs/loader.js:1268:13)
at Object.newLoader [as .js] (/home/lee/Documents/work/item-bank-system/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at Module.require (internal/modules/cjs/loader.js:1140:19)
at require (internal/modules/cjs/helpers.js:75:18)
at Object.<anonymous> (/home/lee/Documents/work/item-bank-system/test/apis/kb/textbook.spec.js:7:49)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Module._compile (/home/lee/Documents/work/item-bank-system/node_modules/pirates/lib/index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
at Object.newLoader [as .js] (/home/lee/Documents/work/item-bank-system/node_modules/pirates/lib/index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:155:15)
at ModuleJob.run (internal/modules/esm/module_job.js:140:23)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at Loader.import (internal/modules/esm/loader.js:165:24)
at Object.exports.loadFilesAsync (/home/lee/Documents/work/item-bank-system/node_modules/mocha/lib/esm-utils.js:33:20)
at singleRun (/home/lee/Documents/work/item-bank-system/node_modules/mocha/lib/cli/run-helpers.js:156:3)
at Object.exports.handler (/home/lee/Documents/work/item-bank-system/node_modules/mocha/lib/cli/run.js:366:5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论