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:
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/mockprocess.exit()somehow?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, likesetTimeout(fn, 999999)? Then again, how do I test whether the parent process exits?