Reputation: 2922
I have some unit tests in mocha, and when I run these only 2/3 of the tests show up.
I am using the speck reporter, and running my tests thru make.
Here is my makefile:
REPORTER = list
TESTS = test/*.js test/**/*.js test/**/**/*.js
REQUIRE = should
test:
@NODE_ENV=test NODE_PATH=./app/controllers ./node_modules/.bin/mocha \
--reporter $(REPORTER) \
--ui tdd \
--require $(REQUIRE) \
-G \
$(TESTS)
test-ci:
@NODE_ENV=ci NODE_PATH=./app/controllers ./node_modules/.bin/mocha \
--reporter $(REPORTER) \
--ui tdd \
--require $(REQUIRE) \
$(TESTS)
start:
NODE_PATH=./app/controllers NODE_ENV=development nodemon server.js
.PHONY: test
And test/test.js
:
/*!
* Module dependencies.
*/
var request = require('supertest')
var app = require('../server')
var mongoose = require('mongoose')
var config = require('../config/config')[process.env.NODE_ENV];
// other stuff you want to include for tests
function clearDB(done) {
(function(done) {
var index = 0;
var models = mongoose.modelNames();
function deleteModel(err) {
if (err) {
return done(err)
};
if (index == models.length) {
return done()
};
mongoose.model(models[index]).remove({}, deleteModel);
index++;
}
deleteModel();
})(done)
}
before(function(done) {
this.timeout(0)
clearDB(done);
})
function populateDatabase(Functions, done) {
Func = mongoose.model("KIFunction");
var functions = 0;
if (typeof Functions == 'function') {
done = Functions
Functions = 20
}
function addFunction(err) {
if (err) {
done(err);
}
if (functions < Functions) {
functions++;
var func = new Func({
string: ("n*" + functions),
approved1: true,
approved2: true,
approved: true,
}).save(addFunction);
} else {
done();
}
}
addFunction();
}
describe('Functions', function() {
describe('GET /functions/get', function() {
it('Should be text/kinoki-function', function(done) {
this.timeout(0)
populateDatabase(11, function(err) {
if (err) done(err)
request(app)
.get("/functions/get")
.query('n=[]')
.expect('Content-Type', new RegExp("kinoki-function"))
.expect(200)
.end(function(err, res) {
if (err) return done(err)
else done()
})
})
})
it('Should have 10 functions', function(done) {
this.timeout(0)
populateDatabase(25, function(err) {
if (err) return done(err)
request(app)
.get("/functions/get")
.query('n=[]')
.expect(200)
.end(function(err, res) {
var body = res.text
body.split("|").length.should.equal(10)
if (err) return done(err)
done()
})
})
it('Each Of Them Should Match The Function Pattern', function(done) {
this.timeout(0)
populateDatabase(11, function(err) {
if (err) return done(err)
request(app)
.get("/functions/get")
.query('n=[]')
.expect(200)
.end(function(err, res) {
var body = res.text
body.split("|").forEach(function(func) {
func.should.match(/[0-9]#n([+\-*\/][0-9]+)+#(Easy|Medium|Hard|Deadly)+#[0-9a-fA-F]{24}/)
})
if (err) return done(err)
done()
})
})
})
it('Should return \'false\' when there are no functions', function(done) {
this.timeout(0)
clearDB(function(err) {
if (err) done(err)
request(app)
.get("/functions/get")
.query('n=[]')
.expect(200)
.end(function(err, res) {
if (err) return done(err)
res.text.should.equal("false")
done()
})
})
})
it('Should NOT throw an error when no array of functions to exclude is in the querystring', function(done) {
this.timeout(0)
clearDB(function(err) {
if (err) done(err)
request(app)
.get("/functions/get")
.end(function(err, res) {
if (err) return done(err)
res.status.should.not.equal(500)
res.status.should.equal(200)
done()
})
})
})
})
describe("POST /functions/submit", function() {
this.timeout(0)
it("Should NOT be a 404", function(done) {
request(app)
.post("/functions/submit")
.end(function(err, res) {
if (err) return done(err)
res.status.should.not.equal(404)
done()
})
})
it("Should be a 400 (Bad Request) without querystring", function(done) {
request(app)
.post("/functions/submit")
.end(function(err, res) {
if (err) return done(err)
res.status.should.equal(400)
done()
})
})
})
})
})
after(function(done) {
// do some stuff
done()
})
And Here is the output from mocha:
03:48 PM Kinoki-Server Ari$ make test Express app started on port 9273
Functions GET /functions/get ✓ Should be text/kinoki-function (67ms) ✓ Should have 10 functions (76ms) POST /functions/submit ✓ Should NOT be a 404 ✓ Should be a 400 (Bad Request) without querystring
4 passing (224 ms)
03:52 PM Kinoki-Server Ari$
It says 4 passing, but I have 6 tests! Why is mocha hiding the other tests?
Upvotes: 1
Views: 4708
Reputation: 146034
My guess is when you directly nest calls to it
inside each other, mocha doesn't get them registered properly. Stick to the pattern of every call to it
must be directly within a describe
callback and see if that fixes it.
Upvotes: 4