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)
    })
  })
Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118
  • I am not sure if you have had a look at the following question, seems a popular issue https://stackoverflow.com/questions/28400459/referenceerror-describe-is-not-defined-nodejs – amyloula Nov 20 '17 at 18:39
  • @loulala Yes I saw this thread, and anothers, but still doesn't work. I updated my question with what I try – Lucas_Santos Nov 20 '17 at 18:44
  • What's the exact command you use to run your tests? – Louis Nov 20 '17 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"` – Lucas_Santos Nov 20 '17 at 18:51
  • Are you using a `mocha.opts` file? If yes, then you should show what it contains in your question. – Louis Nov 20 '17 at 18:55
  • @Louis No, I'm not using `opts` file. I updated my question with some Image, maybe it help – Lucas_Santos Nov 20 '17 at 18:58
  • Are you sure you ran it as a mocha test and did not just run the file as regular Javascript file? Try the command line and run the test from there. – Mörre Nov 20 '17 at 19:11
  • By the command line, it works but I needed to change my package.json to `"scripts": "mocha --recursive"`, but, it shouldn't work with JS file ? – Lucas_Santos Nov 20 '17 at 19:23
  • 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 '17 at 19:28
  • @Lucas_Santos The edit shows you are getting the error in your IDE. And the stack trace you get is *exactly* what you would get if you try to run the test file *outside* Mocha. (Notice how none of the code line references in the trace refer to Mocha.) Your IDE is not invoking the tests the way you think it is. – Louis Nov 21 '17 at 11:46

1 Answers1

-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.