How to test that spawned Node child process is detached and unref()'ed?

Viewed 268

I have a desktop Node.js application that spawns a child process. After spawning, all communications between the parent and child processes go through the network, and I need the processes to be completely independent of each other. spawn()'s option.detached allows for the child process to continue running after the parent exits, and subprocess.unref() allows the parent to exit independently of the child.

While everything works and is pretty easy to manually test (track the processes in Task Manager/Activity Monitor and see what happens when one of the processes is killed/exits), I'm wondering about how I could test this programmatically, specifically using Jest.

There are two testing scenarios:

  1. option.detached – test whether the child process lives even after the parent process exits. But how can I simulate a parent exiting if the parent process is the process Jest tests run in? Perhaps I could simulate/mock process.exit() somehow?
  2. unref() – test whether the parent process can exit without waiting for the child process. Now this is even more difficult and I have no idea how to test this. Perhaps mock the child process with a sample long-lived process, like setTimeout(fn, 999999)? Then again, how do I test whether the parent process exits?
0 Answers
Related