Hi guys
I'm throwing a SOS here. I'm new to nodejs development and to writing tests. I'm trying to write a simple Express app with a small REST-like API to CRUD books, movies etc. The basic boilerplate code works fine, but when I try to execute my (first and basic) test, it throws me an error I can't understand...
Using node 0.8.0 with nvm, express 3, mocha latest 1.3.x on OS X 10.6.8
Here the basic router code :
app.get('/media', function(req, res) {
res.json({});
});
Here's the test using Mocha, request and Chai :
describe('HTTP REST API', function() {
describe('GET "/media"', function() {
it('should return OK (code 200)', function(done) {
request.get(path.join(baseUrl, 'media'), function(err, res, body) {
expect(res.statusCode).to.equal(200);
done();
});
});
});
});
The output is strange, it tells me that 'res' is undefined...
․
✖ 1 of 1 test failed:
1) HTTP REST API GET "/media" should return OK (code 200):
TypeError: Cannot read property 'statusCode' of undefined
at Request._callback (/Users/khalid_jebbari/Sites/mediatheque/test/app.test.js:25:19)
at Request.init.self.callback (/Users/khalid_jebbari/Sites/mediatheque/node_modules/request/main.js:120:22)
at Request.EventEmitter.emit (events.js:88:17)
at Request.init (/Users/khalid_jebbari/Sites/mediatheque/node_modules/request/main.js:167:10)
at new Request (/Users/khalid_jebbari/Sites/mediatheque/node_modules/request/main.js:103:8)
at Function.request [as get] (/Users/khalid_jebbari/Sites/mediatheque/node_modules/request/main.js:939:11)
at Context.<anonymous> (/Users/khalid_jebbari/Sites/mediatheque/test/app.test.js:24:15)
at Test.Runnable.run (/Users/khalid_jebbari/.nvm/v0.8.0/lib/node_modules/mocha/lib/runnable.js:171:15)
at Runner.runTest (/Users/khalid_jebbari/.nvm/v0.8.0/lib/node_modules/mocha/lib/runner.js:300:10)
at Runner.runTests.next (/Users/khalid_jebbari/.nvm/v0.8.0/lib/node_modules/mocha/lib/runner.js:346:12)
at next (/Users/khalid_jebbari/.nvm/v0.8.0/lib/node_modules/mocha/lib/runner.js:228:14)
at Runner.hooks (/Users/khalid_jebbari/.nvm/v0.8.0/lib/node_modules/mocha/lib/runner.js:237:7)
at next (/Users/khalid_jebbari/.nvm/v0.8.0/lib/node_modules/mocha/lib/runner.js:185:23)
at Runner.hook (/Users/khalid_jebbari/.nvm/v0.8.0/lib/node_modules/mocha/lib/runner.js:205:5)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
I don't get it. The response should be a normal response object. I verified it by
1. visiting the url in Chrome and checking the network tab : I do have a 200 answer of type application/json
2. open a node REPL and typing :
I does print '200'
I have no idea why from my test code the app is unreachable whereas it's running in another Terminal tab... I even switched from Chai to Expect.js, same problem. And even rewrote the tests using API-easy. Here's the output from API-easy (it may help...) :
GET "/media" A GET to /http:/localhost:3000/media
✗ should respond with 200
» expected null, got {
message: 'getaddrinfo ENOENT',
stack: 'Error: getaddrinfo ENOENT\n at errnoException (dns.js:31:11)\n at Object.onanswer [as oncomplete] (dns.js:123:16)',
code: 'ENOTFOUND',
arguments: undefined,
type: undefined,
errno: 'ENOTFOUND',
syscall: 'getaddrinfo'
} // dns.js:31
I understand that it may not be related to Mocha but to my machine, but I have no idea why, especially since the app runs just fine, but only the test fails...
Thanks in advance !