Hi,
I have a very simple Koa application:
var app = module.exports = require("koa")();
app.use(function *(){
this.body = "Koa says Hi!";
});
var port = process.env.PORT || (process.argv[2] || 3000);
port = (typeof port === "number") ? port : 3000;
app.listen(port);
console.log("Application started. Listening on port:" + port);
that I test with mocha and supertest like this;
var app = require("../");
var request = require("supertest").agent(app.listen());
describe("Our amazing site", function () {
it("has a nice welcoming message", function (done) {
request
.get("/")
.expect("Koa says Hi!")
.end(done);
});
});
I want to watch my files for changes and use the -w flag like this
mocha -u bdd -R min -w
That works fine. I change a file, the test is reexcuted and all is well.
Now I move that command into my package.json file as a script, like this:
"scripts": {
"watch:test": "mocha -u bdd -R min -w"
},
The first time I run the command it works, when I make a change that is picked up but now the test fails with:
1) Uncaught error outside test suite:
Uncaught Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:837:11)
at exports._exceptionWithHostPort (util.js:860:20)
at Server._listen2 (net.js:1231:14)
at listen (net.js:1267:10)
at Server.listen (net.js:1363:5)
at Application.app.listen (node_modules/koa/lib/application.js:70:24)
at Object.<anonymous> (index.js:10:5)
at Object.<anonymous> (test/site.spec.js:1:73)
at Array.forEach (native)
at StatWatcher._handle.onchange (fs.js:1285:10)
That error will not go away until I stop mocha and then restart it.
Why does it behave differently when run via npm?
What can I do to fix this?
EADDRINUSE means that port is already in use. In you case, your server was not shutdown after the first test run. You should have a teardown hook (after) to shut down the server, stop listening to the port.
Aaah - that would do it. Night is falling here now. I'll check in on how to stop listening to a koa server tomorrow. Keep you posted
Ha - could of course not sleep after that. yes, your are correct. But I had to jump through some Koa-hoops to get there.
I now expose the server returned by app.listen() from the application and use that to close the server in my test.
Server:
var app = require("koa")();
app.use(function *(){
this.body = "Koa says Hi!";
});
var port = process.env.PORT || (process.argv[2] || 3000);
port = (typeof port === "number") ? port : 3000;
var server = app.listen(port);
console.log("Application started. Listening on port:" + port);
module.exports = {
server : server,
app : app
};
Test:
var app = require("../").app;
var server = require("../").server;
var request = require("supertest").agent(server);
describe("Our amazing site", function () {
after(function (done) {
server.close();
done();
});
it("has a nice welcoming message", function (done) {
request
.get("/")
.expect("Koa says Hi!")
.end(done);
});
});
Thanks a bunch! Recording a screen cast for PluralSight and this stopped me in my tracks
No problemo!
And thanks much for posting back the solution! I tried finding the way to close the server to paste it back here and couldn't find it in less than 5min. I was left curious. :smile:
I wrote the whole thing down in a blog post, should anyone want to know more.
What I still fail to grasp is why this only failed when run as a script in npm. Running the same mocha command from the prompt worked fine
Why are error messages being suppressed? I can't figure out why I'm seeing this error. Is there any way to debug this? I tried the blog post, but maybe that API is out of date?
This has saved me, I will spread the news!!!
Most helpful comment
I wrote the whole thing down in a blog post, should anyone want to know more.
What I still fail to grasp is why this only failed when run as a script in npm. Running the same mocha command from the prompt worked fine