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)
阅读 1.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏