'Describe is not defined' when running my test

Viewed 11792

I'm getting the following error when running my test case with mocha

ReferenceError: describe is not defined
    at Object.<anonymous> (/myproject/test/unit/physicalperson.spec.js:12:1)

Someone knows why this is happens?

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

What I've Try

I saw some threads here in SO, and I also try the following code below, but the same error occurs.

  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 Answers

I suppose, you are running the test with node command ,example:node fileName.test.js That won't work .For mocha to understand its a test file ,you have to run it with mocha command as below.

mocha fileName.test.js

Or simply configure in your package.json file to run the test with **npm run test** command ;example follows.

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

The above command generates the coverage report too with istanbul.

Related