“undeclared variable” warning in Mocha tests in Cloud9

I'm new at node.js and the framework Mocha for unit testing, but I've created a couple of tests in cloud9 IDE just to see how it works. The code looks like this:

var assert = require("assert");
require("should");

describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    });
  });
});

describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return the index when the value is present', function(){
      assert.equal(1, [1,2,3].indexOf(2));
      assert.equal(0, [1,2,3].indexOf(1));
      assert.equal(2, [1,2,3].indexOf(3));
    });
  });
});

The tests work if I type mocha in the console, but the IDE shows warnings in the lines where "describe" and "it" are because it says that the variable has not been declared ("undeclared variable").

I wonder what should I do these tests to avoid the warnings.

Thanks.

Answers:

Answer

In cloud9 you can add a hint for globals as a comment at the top of the file and it will remove the warnings. e.g.

**/* global describe it before */**

var expect = require('chai').expect;


describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
        expect(true).to.equal(true);
    })
  })
})
Answer

That's because mocha "executable" wraps your test in requires needed to use mocha functions (describe, and it). Take a look at mocha and _mocha in your node_modules/mocha/bin directory.

On the other hand cloud9 tries to resolve all symbols using a pure node executable, so you have to require everything by hand.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.