3

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)
    })
  })
10
  • I am not sure if you have had a look at the following question, seems a popular issue stackoverflow.com/questions/28400459/…
    – amyloula
    Nov 20, 2017 at 18:39
  • @loulala Yes I saw this thread, and anothers, but still doesn't work. I updated my question with what I try Nov 20, 2017 at 18:44
  • What's the exact command you use to run your tests?
    – Louis
    Nov 20, 2017 at 18:48
  • @Louis npm test, and in package.json I put "scripts": { "test": "mocha" } , but I've try also "test": "node ./node_modules/mocha/bin/mocha" Nov 20, 2017 at 18:51
  • 1
    I don't understand the 2nd half of your reply. It is clear that you are not calling the tests (i.e. through mocha) but run the files as JS files. Of course that won't work. Check out the mocha documentation on how to run tests.
    – Mörre
    Nov 20, 2017 at 19:28

1 Answer 1

-1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.