'描述未定义'在运行我的测试时

时间:2017-11-20 18:33:15

标签: node.js mocha

使用mocha

运行我的测试用例时出现以下错误
ReferenceError: describe is not defined
    at Object.<anonymous> (/myproject/test/unit/physicalperson.spec.js:12:1)

有人知道为什么会这样吗?

package.json

"scripts": {
    "test": "mocha"
  },

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);
        });
    });
  });
});

enter image description here enter image description here

我尝试过什么

我在SO看到了一些帖子,我也尝试下面的代码,但是会出现同样的错误。

  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)
    })
  })

1 个答案:

答案 0 :(得分:0)

我想,您正在使用节点命令来运行测试,例如: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\"",

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

相关问题