Rainer Plumer
Rainer Plumer

Reputation: 3753

How to start node server before running end to end tests using npm run?

I'm writing end to end tests for an express site, and I want to add a "test" command into package.js

This command needs to:

  1. run eslint
  2. compile typescript
  3. start node server
  4. run unit tests against that server and show output.
  5. once done testing, close the server.

I know how to execute all those commands individually, but not all at once.

What I have now is :

npm run compile && npm run build && node ./dist/server.js --db=test && npm run test

It works to the point of: "&& npm run test" since node server is running, it won't continue on to the next command, and if it closes then tests wouldn't run.

Any help is appreciated.

Upvotes: 4

Views: 1696

Answers (1)

dm03514
dm03514

Reputation: 55922

One thing that I have found to help with reliable, maintainable end-to-end tests is to separate concerns:

  • Test suite assumes that the server is already running
  • Orchestrator calls into separate commands to bring up your test stack then run the tests

In CI, this could look like

npm start-e2e-test-stack --port=XXXX --db=test
npm test --port=XXXX --db=test
npm teardown-e2e-test-stack

In my experiences, having the end-to-end tests operate against any server helps to allow them to verify all environments, local, dev, qa, staging, production.

Upvotes: 1

Related Questions