7

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.

Juanillo
  • 875
  • 2
  • 11
  • 17

2 Answers2

3

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);
    })
  })
})
lummie
  • 31
  • 2
  • 1
    I think that the correct syntax is `/* global describe it before */`. See also http://stackoverflow.com/a/19572515/209727 – Davide Icardi Apr 19 '15 at 17:18
0

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.

soulcheck
  • 36,297
  • 6
  • 91
  • 90
  • so what should I require by hand? Because I've tried to do var mocha = require("mocha"); and change the code to mocha.describe... and the warnings dissapear, but when executing mocha it fails. – Juanillo Nov 28 '12 at 10:08
  • @Juanillo I'm not sure there's an easy workaround for that. You can force cloud9 to run your mocha tests but that doesn't solve the undeclared variables problem. – soulcheck Nov 28 '12 at 10:53
  • ok, thanks, I was curious about this, and as I'm newbie at this platform yet I was wondering if the experienced programmers do something in this case. But it seems other real projects use Mocha with similar codes. Maybe they just ignore the warnings while working in cloud9, though it's really surprising to me that a code can be given with warnings in a really serious projects. – Juanillo Nov 28 '12 at 11:17