I'm using Jasmine 2.3
installed via NPM and executed with Grunt.
'use strict';
module.exports = function(grunt) {
grunt.initConfig({
package: grunt.file.readJSON('package.json'),
exec: {
jasmine: 'node_modules/.bin/jasmine'
}
});
require('load-grunt-tasks')(grunt);
require('time-grunt')(grunt);
grunt.registerTask('default', 'exec:jasmine');
};
I exported an Express.js application object and using it in my specs along with SuperTest.
'use strict';
var supertest = require('supertest')
var application = require('../../../../../server');
describe('GET /api/users', function() {
it('should respond with json', function(done) {
supertest(application)
.get('/api/users')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, done);
});
});
When I run the spec I get no errors even though a 200
status code was expected and 404
was the result. Is the problem in Jasmine or SuperTest, or maybe I should be using SuperAgent.
I have no routes setup just a 404
error handler setup on the Express application object.
application.use(function(request, response, next) {
response
.status(404)
.send({
message: 'not found'
});
});
Firstly, Good question! I've learned some things researching this. Apparently Jasmine + Supertest don't play very well together. The reason for this appears to be Supertest calling done(err)
, but Jasmine will only fail when done.fail()
or fail()
is called. You can see some github issues here, and here.
Use this code to see proof:
describe('GET /api/users', function() {
it('should respond with json', function(done) {
supertest(application)
.get('/api/users')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200, function(res){
console.log(res);
//res is actually an error because supertest called done(error)!
done.fail() //Now jasmine will actually fail
});
});
});
Given this, it appears the easiest solution is to use an alternate library that play nicely together. I've personally used mocha instead of jasmine, and had good success. The choice is yours!
If you really wanted to, you could use jasmine and write your own validators seen in the supertest documentation here.
I hope this helps!