Node.js ';描述未定义';当运行我的测试时

Node.js ';描述未定义';当运行我的测试时,node.js,unit-testing,mocha.js,Node.js,Unit Testing,Mocha.js,使用运行测试用例时出现以下错误 physicalperson.spec.js const request = require('supertest-as-promised'); const chai = require('chai'); const app = require('../../src/server'); const expect = chai.expect; const PHYSICALPERSON_ENDPOINT = '/api/physicalperson'; descr

使用运行测试用例时出现以下错误

physicalperson.spec.js

const request = require('supertest-as-promised');
const chai = require('chai');
const app = require('../../src/server');

const expect = chai.expect;
const PHYSICALPERSON_ENDPOINT = '/api/physicalperson';

describe('Integration tests for Physical Person', () => {
  describe('\nFAIL Cases - Physical Person', () => {
    it('should return 404 status if physical person is not present on database', (done) => {
      request(app)
        .get(`${PHYSICALPERSON_ENDPOINT}/xxap`)
        .then((res) => {
          expect(res.statusCode).to.equal(404);
          done();
        })
        .catch((err) => {
          done(err);
        });
    });
  });
});

我所尝试的

我在中看到了一些线程,我也尝试了下面的代码,但出现了相同的错误

  var mocha = require('mocha')
  var describe = mocha.describe
  var it = mocha.it
  var assert = require('chai').assert

  describe('#indexOf()', function() {
    it('should return -1 when not present', function() {
      assert.equal([1,2,3].indexOf(4), -1)
    })
  })

我假设您正在使用node命令运行测试,例如:
node fileName.test.js
这是行不通的。为了让mocha理解它是一个测试文件,您必须使用mocha命令运行它,如下所示

mocha fileName.test.js
或者只需在package.json文件中配置以使用
**npm run test**
命令运行测试;下面是一个例子

"test": "istanbul cover --report cobertura --report html ./node_modules/mocha/bin/_mocha -- --reporter mocha-jenkins-reporter -t 180000 \"test/unit/*.js\"",

上面的命令也会生成伊斯坦布尔的覆盖率报告。

我不确定您是否看过下面的问题,似乎是一个热门问题@loulala是的,我看到了这个线程和其他线程,但仍然不起作用。我用什么更新了我的问题?你运行测试的确切命令是什么?@Louis
npm test
,在package.json中我放了
“scripts”:{“test”:“mocha”}
,但我也尝试了
“test”:“node./node\u modules/mocha/bin/mocha”
我不明白你回答的下半部分。很明显,您没有调用测试(即通过mocha),而是将文件作为JS文件运行。那当然不行。查看有关如何运行测试的mocha文档。
mocha fileName.test.js
"test": "istanbul cover --report cobertura --report html ./node_modules/mocha/bin/_mocha -- --reporter mocha-jenkins-reporter -t 180000 \"test/unit/*.js\"",