node.js - mocha watching fails under npm -


i have 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 test mocha , supertest this;

var app = require("../"); var request = require("supertest").agent(app.listen());  describe("our amazing site", function () {      it("has nice welcoming message", function (done) {         request             .get("/")             .expect("koa says hi!")             .end(done);     }); }); 

i want watch files changes , use -w flag this

mocha -u bdd -r min -w 

that works fine. change file, test reexcuted , well.

but, strangely, if move command package.json file script, this:

"scripts": {     "watch:test": "mocha -u bdd -r min -w" }, 

the first time run command works, when make change picked test fails with:

1)  uncaught error outside test suite:      uncaught error: listen eaddrinuse :::3000       @ object.exports._errnoexception (util.js:837:11)       @ exports._exceptionwithhostport (util.js:860:20)       @ server._listen2 (net.js:1231:14)       @ listen (net.js:1267:10)       @ server.listen (net.js:1363:5)       @ application.app.listen (node_modules/koa/lib/application.js:70:24)       @ object.<anonymous> (index.js:10:5)       @ object.<anonymous> (test/site.spec.js:1:73)       @ array.foreach (native)       @ statwatcher._handle.onchange (fs.js:1285:10) 

that error not go away until stop mocha , restart it.

why behave differently when run via npm? can fix this?

ok - found solution. has i'm starting app twice, when under test. , not closing both.

to start testing supertest construct request this: var request = require("supertest").agent(app.listen());. btw app.listen() same thing in our application.

since watching our files changes server never gets close. on next run of test starts again: var request = require("supertest").agent(app.listen()); , "address in use".

the solution simple: start listening when not running under test. simple way checking module parent in application:

if(!module.parent) {    app.listen(); } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -