I want to run tests on my node express server however this application is starting the server like this:
createServer()
.then(server => {
server.listen(PORT);
Log.info(`Server started on http://localhost:${PORT}`);
})
.catch(err => {
Log.error("Startup failure.", { error: err });
process.exit(1);
});
and I know the chai.request() needs to have a parameter that is pointing toward the server app, how can I export/import this createServer() function and pass it in the request method of the chai object?
You can use require.main to distinguish between your files being run directly by Node or imported as modules.
E.g.
server.js:When you import the
createServerfrom theserver.jsfile, theifstatement block will not execute. Because you want to create the server instance in the test case.server.test.js: