Reputation: 967
I write the a simple unit test with mocha, chai and supertest.
describe('controller.CWEManagementAPI', function () {
it('should be able to say hello', function() {
var request = require('supertest')
, express = require('express');
var app = express();
app.get('/user', function(req, res){
res.send(201, { name: 'tobi' });
});
request(app)
.get('/user')
.set('Accept', 'application/json')
.expect(200)
.end(function(err, res){
if (err) return done(err);
console.log('test');
assert.equal( res.body.name, 'tobi');
done()
});
});
});
But the problem is that : the console.log('test')
is not executed.
So I think the assert.equal( res.body.name, 'tobi');
is not executed either.
And so I write the code without unit testing, like :
var request = require('supertest')
, express = require('express');
var app = express();
app.get('/user', function(req, res){
res.send(201, { name: 'tobi' });
});
request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '20')
.expect(201)
.end(function(err, res){
if (err) throw err;
console.log(res.body.name);
console.log('done');
process.exit();
});
And the console.log()
are all executed. so I don't know why the first code can not show the logging info.
Upvotes: 2
Views: 5642
Reputation: 1534
What you are trying to run is an asynchronous test with mocha. So the test case should receive a callback parameter to invoke once it's done with its actions.
You are invoking done() at the end but haven't received it as a parameter.
change
it('should be able to say hello', function() {
}
to
it('should be able to say hello', function(done) {
}
Upvotes: 9