Tee up a test server in one file, tear it down in another
This makes it easy to set up a server or child process in a test file
00-setup.js or something, and then tear it down in
zz-teardown.js, so that you can get the benefits of running your
tests in separate files, but still bang on the same server across the
You may have a setup file that starts a server, like so:
// 00-setup.jsvar tup =
Then, a bunch of tests that do things to that server:
// test-ping.jsvar t =var http =t
And of course, a negative test:
Last but not least, tear down the server when you're done:
// zz-teardown.jsvar tup =tup
- The module that calls
t-upshould probably not do anything else. It's going to be run a second time in a child process, so any other side-effects are bad.
- You should use tap for your tests, since this module will output some TAP notes about how it's going.
- Definitely clean up after yourself! If you don't ever call
tup.close(), then you'll have a bunch of node processes lying around.
- The server MUST call the
done()function when it's ready to move onto the next test. This writes a special key to standard output, and prevents a race condition where the tests fail because the server isn't up yet. Otherwise, all stdio is completely lost, because the child process is abandoned without access to that stuff.