Search code examples
node.jsexpressmocha.jschaisupertest

What does app.listen look like when using npm start


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.


Solution

  • 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: