0

So this is what I was doing before I exported the app variable so I can perform unit and integration testing for node/express app

app.js

//app.listen(3000, () => console.log('Example app listening on port 3000!'))

module.exports = app;

Now

How do I start the server now? I was trying to move it to package.json file in the

"scripts": {"start": "app.listen(3000, () => console.log('Example app listening on port 3000!')" }

so I can do:

npm start

but it's not working.

Rod
  • 14,529
  • 31
  • 118
  • 230

1 Answers1

1

That won't work. You can have a index.js who will start your app, like this:

index.js

import app from './app'

app.listen(3000, () => console.log('Example app listening on port 3000!'))

And your script start should be:

"scripts": {"start": "node index.js" }

Like this, you can use your module app in your tests, and your application will work properly.

Now, just run:

npm start

If you wan't to do unit and integration tests, take a look at these links:

reisdev
  • 3,215
  • 2
  • 17
  • 38