"undeclared variable" warning in Mocha tests in Cloud9 - javascript

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.

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);
})
})
})

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.

Related

How to use should.js as global variable:

I am trying to write some unit test that using mocha and should.js since I would like to keep format for each of my unit test the same and each unit test require should.js to verify proerpty of object. How can I make it as globally variable so I do not need to require should.js on each test file so far I have tried
global.should = require('should');
describe('try gloabl'), () => {
it('should work'), () => {
let user = { name: 'test'};
global.should(user).have.property('name', 'test');
});
});
#error, global.should is not a function
and if i use this. it works
const should = require('should');
should = require('should');
describe('try gloabl'), () => {
it('should work'), () => {
let user = { name: 'test'};
global.should(user).have.property('name', 'test');
});
});
First of all, I'm tired of writing "require" is the worst reason to use the GLOBAL variable. There is a reason that using require is the conventional way of doing things, and it is not different from any other language where you have to import or type using in each and every file. It just makes it easier to understand what the code is doing later on.
See this for further explanation.
Now, that said, when requiring should, the module actually attaches itself to the GLOBAL variable, and makes describe, it and should accessible methods.
index.js
require('should');
describe('try global', () => {
it('should work with global', () => {
let user = { name: 'test' };
global.should(user).have.property('name', 'test');
});
it('should work without global', () => {
let user = { name: 'test' };
should(user).have.property('name', 'test');
});
});
//////
mocha ./index.js
try global
√ should work with global
√ should work without global
2 passing (11ms)
I fixed the typos in your code (eg. Removing the extra ) from describe and it function), and this code works just fine when running with mocha ./index.js. Make sure you have install mocha with npm i -g mocha to make the module available in the CLI.

Chai assertions not executed inside JSDOM Env block

I've just started writing my front-end unit tests with mocha, chai and jsdom. When I try to follow the tutorials based on those - I'm getting passing tests even when I set them to fail. Everything else is setup and works as expected jQuery, setup.js, window etc. The only issue is that my Assertions are not getting executed at all inside the jsdom env block.
This is my test:
var chai = require('chai'),
expect = chai.expect,
jsdom = require('jsdom'),
fs = require('fs'),
jquery = fs.readFileSync('./js/vendor/jquery-3.0.0.min.js', 'utf-8');
describe('UI DOM tests', function () {
it('should fail', function () {
// simple html
var htmlFragment = fs.readFileSync('./test/runner.html');
jsdom.env({
html: htmlFragment,
src: [jquery, '../node_modules/chai/chai.js'],
done: function (err, window) {
var $ = window.$;
expect(true).eql(false); // nothing happens
done();
}
});
expect(true).eql(false); // assert fails as expected
});
});
Any help is greatly appreciated.
it seems you're missing the done argument in your it:
it('should fail', function (done) {
otherwise mocha will think your it is synchronous and finish before your jsdom env is created.

before/afterAll() is not defined in jasmine-node

I'm trying to use the methods beforeAll and afterAll of jasmine, to create a suite of tests with frisby.js, because actually, frisby doesn't have a support for this methods. So, this is what I'm trying to do:
var frisby = require('frisby');
describe("setUp and tearDown", function(){
beforeAll(function(){
console.log("test beforeAll");
});
afterAll(function(){
console.log("afterAll");
});
//FRISBY TESTS
}); //end of describe function
If I change the methods before/afterAll to before/afterEach, is working, but when I'm using before/afterAll this error appears on console:
Message:
ReferenceError: beforeAll is not defined
Stacktrace:
ReferenceError: beforeAll is not defined
I have the jasmine version 2.3.2 installed on my project, so, I don't know what I need to do to integrate this method.
Use the jasmine library not the jasmine-node library. The second one does not support beforeAll and afterAll methods.
1- npm install -g jasmine
2- jasmine init
3- write the test in the spec folder:
describe("A spec using beforeAll and afterAll", function() {
var foo;
beforeAll(function() {
foo = 1;
});
afterAll(function() {
foo = 0;
});
it("sets the initial value of foo before specs run", function() {
expect(foo).toEqual(1);
foo += 1;
});
it("does not reset foo between specs", function() {
expect(foo).toEqual(2);
});
});
4- Run the tests --> jasmine
The current version of frisby doesnt suport this kind of setup. The community, like myself is eager to this feature like in this issue describes.
The team is working on this feature, but it will come in version 2 of the package that is in the way for more than a year now. More info at this link.

requirejs mocha context error

I have a simple mocha test that fails when using requirejs and the context config.
Here's A.js
define([], function(){
return {};
});
Here's the test spec.js
var requirejs = require('requirejs');
var localReq = requirejs.config({
baseUrl: "./",
context: "context1"
})
describe("context test", function () {
it("should not throw error", function () {
for (var i = 0; i < 100; i++) {
console.log(localReq("A"), i);
}
});
});
When I run the test mocha spec.js, I get the following error:
Uncaught Error: Tried loading "A" at /Users/khirakawa/work/test/node_modules/mocha/bin/A.js then tried node's require("A") and it failed with error: Error: Cannot find module 'A'
Here's a screenshot:
Notice how A was properly loaded and logged 100 times, yet the test still failed. If I comment out the context config, it works just fine.
Mocha is also printing out '1 passing' and '1 failing', even though there is only 1 test.
Why is this happening?
You could write your test like this:
describe("context test", function () {
it("should not throw error", function (done) {
localReq(["A"], function (f) { done(); });
});
});
As you pointed out in a comment, calling localReq to get a module synchronously should work but for some unexplained reason it does not. The code above, which calls localReq to load the module asynchronously, works.
The reason Mocha is saying that your single test is passing and failing is that it is detecting an error that happens after your test is over and has no other test to associate it with. This kind of error message where the same test both passes and fails is a sure indication that you've got something happening asynchronously but that you have not taken care of it in your Mocha test setup.

Chai exports are not found in Mocha test

I have created simple Mocha test. It works perfectly when Node "assert" module is used. I run it from command line (Mocha is installed as a global node module):
$ mocha myTest.js
․
1 test complete (6 ms)
The script looks like this:
var assert = require("assert")
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));
})
})
})
Well, I tried to add Chai instead of assert library. I installed it first:
npm install chai
So, the directory node_modules has been created in my project. Great so far. Then, I altered the script to use Chai:
var chai = require("chai");
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
[1,2,3].indexOf(5).should.equal(-1);
expect([1,2,3].indexOf(5)).to.equal(-1);
assert.equal([1,2,3].indexOf(5),-1);
})
})
});
It does not work, Mocha test fails with TypeError:
TypeError: Cannot call method 'equal' of undefined
I assume Chai did not define should so it is undefined.
How is this possible?
How can I make my tests run with Chai? I have tried to install Chai globally with no effect. I also ran the script with -r chai with no effect as well.
Apparently, Chai module is loaded, but does not define the variables (Object.prototype properties). How can I fix this?
var expect = require('chai').expect;
That will get your expect calls working. However, you also have a should call which comes from a different library entirely, so change
[1,2,3].indexOf(5).should.equal(-1);
to
expect([1,2,3].indexOf(5)).to.equal(-1);

Categories