Node.js Mocha错误:ReferenceError:未定义测试



运行测试脚本后出现错误。

app.test.js

const { app } = require('../src/app');
describe('Basic functionality', () => {
test('App should receive a command to run', () => {
expect(app('1'))
.toEqual('Run command 1');
});
test('Should return instructions when no command is given', () => {
expect(app())
.toEqual('Usage: node index.js <COMMAND>');
});
test('Should throw an error when no such command exists', () => {
expect(() => { 
app('100')
})
.toThrow('No such command exists');
});
});

package.json(我已经在我的项目中安装了mocha。我试图在全球范围内安装mocha,但结果是一样的(

"scripts": {
"test": "mocha __tests__/app.test.js"
},
"devDependencies": {
"mocha": "^8.0.1"
}

运行后出现错误

纱线运行测试

下面是错误代码

ReferenceError: test is not defined
at Suite.describe (/test/__tests__/app.test.js:4:2)
at Object.create (/test/node_modules/mocha/lib/interfaces/common.js:143:19)
at context.describe.context.context (/test/node_modules/mocha/lib/interfaces/bdd.js:42:27)
at Object.<anonymous> (/test/__tests__/app.test.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.exports.requireOrImport (/test/node_modules/mocha/lib/esm-utils.js:20:12)
at Object.exports.loadFilesAsync (/test/node_modules/mocha/lib/esm-utils.js:33:34)
at Mocha.loadFilesAsync (/test/node_modules/mocha/lib/mocha.js:421:19)
at singleRun (/test/node_modules/mocha/lib/cli/run-helpers.js:156:15)
at exports.runMocha (/test/node_modules/mocha/lib/cli/run-helpers.js:225:10)
at Object.exports.handler (/test/node_modules/mocha/lib/cli/run.js:366:11)
at innerArgv.then.argv (/test/node_modules/yargs/lib/command.js:241:49)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

请教我哪里错了。

我通过指定路由解决了这个问题:

像这样:

npx mocha "src/testing-examples/index.test.js" --recursive --require @babel/register  

我希望你觉得它有用:(

最新更新