3

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.

dm03514
  • 54,664
  • 18
  • 108
  • 145
Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42

1 Answers1

1

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
dm03514
  • 54,664
  • 18
  • 108
  • 145