NodeJS 错误:引用错误:未定义测试

c3frrgcw  于 2022-11-29  发布在  Node.js
关注(0)|答案(1)|浏览(120)

运行测试脚本后出现错误。
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"
  }

运行后出现错误
Yarn运行试验
下面是错误代码

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)

请告诉我我哪里做错了。

aoyhnmkz

aoyhnmkz1#

我通过指定路线解决了这个问题:
就像这样:

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

希望你觉得它有用:)

相关问题