0

I've followed some tutorials and web casts on code school, mainly those on Node, ES2015, Angular, and Express. I've started a little project of my own and I've implemented all of the above. I'm also trying to get automated builds and testing working using Travis-CI and Mocha/Supertests for test driven development. The problem though is if I run npm test I get this error from Mocha

1)  Uncaught error outside test suite:
     Uncaught Error: listen EADDRINUSE :::3000
      at Object.exports._errnoException (util.js:870:11)
      at exports._exceptionWithHostPort (util.js:893:20)
      at Server._listen2 (net.js:1236:14)
      at listen (net.js:1272:10)
      at Server.listen (net.js:1368:5)
      at EventEmitter.listen (node_modules/express/lib/application.js:617:24)
      at Object.<anonymous> (server/app.js:34:5)
      at require (internal/module.js:12:17)
      at Object.<anonymous> (test.js:6:11)
      at require (internal/module.js:12:17)
      at Array.forEach (native)
      at node.js:962:3

I just noticed that I was missing module.exports = app at the end of my main app.js that might have helped but the tests still fail.

Here is a link to the latest push on GitHub.
Here is the failing build on Travis-CI.

ocean
  • 1,335
  • 15
  • 26
gh0st
  • 1,653
  • 3
  • 27
  • 59

3 Answers3

1

Changing my server/app.js to listen if using

if (!module.parent) {
    app.listen(port, () => {
        console.log("Listening on port " + port);
    });
}

Resolved my issue regarding testing not working on my local machine. The reason behind doing this can be found here.
Also adding

services:
    - mongodb

like @ocean said worked, which reasoning can be referenced here.

gh0st
  • 1,653
  • 3
  • 27
  • 59
0

The error you're currently getting on Travis CI is because NPM is trying to start your app with node server/app.js (as specified in line 9 of your package.json), but your app requires MongoDB to be running (as specified on line 13 of your app.js).

NPM has to start your app before it can run the tests.

MongoDB is available as a service on Travis CI. Have a look at the Travis CI Database Setup docs and see how you go.

ocean
  • 1,335
  • 15
  • 26
  • You're right and I'll probably have that error when I get to that point too but currently I'm trying to test using mocha on my local machine and I get this error. – gh0st Apr 01 '16 at 15:06
  • I resolved my issue regarding testing not working locally. I'm pretty sure that didn't have anything to do with my Travis-CI failing but I fixed it anyway and after I did travis continued to fail. Once I followed your suggestion of adding mongodb to the `.travis.yml` Travis builds began working. – gh0st Apr 06 '16 at 19:36
0

Ok then, well the error is that this Express app is trying to use a network port on your machine which is already being used by something else (maybe another Node / Express app, as they often use 3000).

Try changing the var port declaration near the bottom of server/app.js to be something other than 3000.

ocean
  • 1,335
  • 15
  • 26
  • Hmm, weird. What did you change the port variable to? And have you pushed the latest code to GitHub so we can have a look at it? – ocean Apr 05 '16 at 02:14
  • Changed the port variable to 3030. Latest code is on Github. https://github.com/gh0st/ncps-mms/commit/9efa710d5d9d87926d5f967378ea894d5b47414a – gh0st Apr 05 '16 at 03:59
  • Ok, no idea. I cloned your repo, ran `npm install`, then ran up a MongoDB in another terminal window, then ran `npm test` and it worked fine. I also tried running the tests with the app already running (using `npm start`) in another terminal window, and the test ran fine then too. – ocean Apr 08 '16 at 02:35
  • I know. I fixed it. See my answer below. – gh0st Apr 08 '16 at 02:36
  • Maybe your Node module versions are out of whack. Try deleting your `node_modules` subdirectory inside the app repo, and then re-running `npm install`. – ocean Apr 08 '16 at 02:37
  • @gh0st Aha, didn't see that, whoops ;-) – ocean Apr 08 '16 at 02:38