Reputation: 32104
I'm trying to put together a supertest
-based integration test suite (run by Mocha) that pings our REST API and validates the response.
However, my test doesn't seem to be running as expected:
var assert = require('assert')
var should = require('should')
var request = require('superagent')
var WEBSERVICE_BASE = 'localhost:8080/our-application/'
describe('Authentication', function() {
it('prevents user from logging in without credentials', function() {
console.log('###')
console.log('Starting with: ' + request)
console.log('###')
request.get(WEBSERVICE_BASE + 'auth', function(err, res) {
console.log('Error: ' + err)
if (err) {
throw err
}
res.should.have.status(401)
done()
})
})
})
What I see in the console:
Craigs-MBP:mocha-tests Craig$ ./run.sh
Authentication
###
Starting with: function request(method, url) {
// callback
if ('function' == typeof url) {
return new Request('GET', method).end(url);
}
// url first
if (1 == arguments.length) {
return new Request('GET', method);
}
return new Request(method, url);
}
###
✓ prevents user from logging in without credentials
1 passing (12ms)
It seems request
is being redefined as a function, instead of the superagent
object?
The test should not pass, and at the very least the console.log
printing the err
parameter should be seen.
Upvotes: 1
Views: 306
Reputation: 22553
Remember, javascript is asynchronous. Superagent terminates the test before your callback is called unless you put the "done" parameter in the it method like so:
it('prevents user from logging in without credentials', function(done) {...
The test finished executing and mocha terminated before the callback ever gets called.
Upvotes: 2