jeudi 21 janvier 2016

How to enforce errors in a child process for testing purposes in node.js

I have an API written in node.js. When an unexpected error occurs, my API finishes off all requests, rejects new requests with a 503 and then shuts down the process.

I would like to write some tests for this behaviour. However, I am unsure how I could create a route that throws an error without hard-coding it into my code.

I don't want to hard-code routes for testing purposes as this adds complexity and is very troubling if accidentally exposed in production. If my tests were running in the same process as the server, I could just use something like proxyquire to inject some modules that behave badly but then I couldn't test for the server to exit and if it does exit, it would crash my tests.

So:

  • my tests are running a node.js in a child process
  • I want to send a request to a route that triggers an error
  • This error route should not be included in my application code
  • I need to test that:
    • the server exits
    • it declines new requests
    • it waits for pending requests to finish

Any ideas? Thanks a lot for your help!

Max

Aucun commentaire:

Enregistrer un commentaire