-
-
Notifications
You must be signed in to change notification settings - Fork 358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recommended way of terminating subprocess #762
Comments
The approach above is only part of the solution. One's Mocha code should also wait until the subprocess emits the nyc was intermittently not picking up the coverage from the subprocess. My guess is sometimes Mocha terminated before the data from the subprocess finished writing to disk. Is this what other people have had to do or am I way off base? Here's a test.js that works with the files above. test.js/* ... */
let expressApp;
// Runs before all test suites
before(function(done) {
expressApp = child_process.fork("app.js", [], {stdio:"pipe"});
// Wait until the Express app is listening
expressApp.stdout.on("data", data => {
if (data.toString().includes("ModelApp listening"))
done();
});
});
// Runs after all test suites
after(async function() {
await new Promise(resolve => {
expressApp.on("exit", () => resolve());
expressApp.send("Dear Express, Please exit. Sincerely, Mocha");
});
});
// Our tests
describe("My Tests", function() {
/* Do tests */
}); |
@SuperFun99 when testing web-apps, the approach I've tended to use is to stand up a server in the |
Thanks for the idea @bcoe but using |
@SuperFun99 I believe this app uses restify rather than Express, but it's a good example of the approach I've used in the past for testing web-applications with coverage: https://github.com/npm/oauth2-server-pg/blob/master/test/server.js#L14 while we could get your current approach working, I think it might be a bit easier to forego the |
I got also the problem to have the coverage when I was launching nyc over a long living server (express). I ended by finding a solution thanks to how we handled a sub process that returned the coverage. I added the following code to listen to process.on('SIGINT', function() {
process.exit()
}) As a result, nyc was printing the coverage. |
I'm using Mocha to test an Express app. In a global
before()
, Mocha starts the Express app usingchild_process.fork()
and then sends the server HTTP requests. In a globalafter()
, I terminate the Express app.I had trouble at first when I was using
subprocess.kill()
to terminate it. nyc wasn't reporting any results because it apparently wasn't receiving them from the subprocesss.After much experimentation I discovered that it works if I send the Express app a message
subprocess.send("You can stop now")
and then have it terminate itself.Is this the recommended way to handle subprocesses? Very slick how nyc automatically covers them. It'd be nice though if the parent processes could just kill them willy-nilly without having to add code to the app being tested.
Details
For those that may come after, here's my current working setup:
app.js
test.js
package.json
The text was updated successfully, but these errors were encountered: