'TypeError: mocha.run is not a function

I am using mocha "mocha": "^7.2.0" to do an unit test in the typescript project, this is the test code time.test.ts looks like:

import mocha from 'mocha'
var expect = require('chai').expect;

mocha.setup('bdd');

describe('Add', function() {
  it('1 + 1', async function() {
    expect(1+ 1).to.be.equal(2);
  });
});

mocha.run();

when I run this code using command:

mocha -r esm test/**/** test/utils/data/**

shows error:

$ yarn test                                                                                                                                         ‹ruby-2.7.2›
yarn run v1.21.1
$ mocha -r esm test/**/** test/utils/data/**

/Users/dolphin/source/reddwarf/frontend/js-wheel/test/utils/time.test.ts:1
TypeError: mocha.run is not a function
    at Object.<anonymous> (/Users/dolphin/source/reddwarf/frontend/js-wheel/test/utils/time.test.ts:12:7)
    at Generator.next (<anonymous>)
    at Object.m._compile (/Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/ts-node/src/index.ts:1475:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/ts-node/src/index.ts:1478:12)
    at requireOrImport (/Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/esm-utils.js:15:12)
    at Object.exports.loadFilesAsync (/Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/esm-utils.js:28:26)
    at Mocha.loadFilesAsync (/Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/mocha.js:386:19)
    at singleRun (/Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/cli/run-helpers.js:149:15)
    at exports.runMocha (/Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/cli/run-helpers.js:186:11)
    at Object.exports.handler (/Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/mocha/lib/cli/run.js:319:11)
    at /Users/dolphin/source/reddwarf/frontend/js-wheel/node_modules/yargs/lib/command.js:241:49
    at process.runNextTicks [as _tickCallback] (node:internal/process/task_queues:61:5)
    at node:internal/main/run_main_module:17:47
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

why did this happen? what should I do to fix this problem? the node version is v16.13.2.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source