mocha 测试 中使用 async 出现错误

发布于 2022-09-12 04:43:49 字数 2755 浏览 32 评论 0

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文