0

I'm having issues keeping my node child process alive. I have one complex webserver running express in a typescript file index.ts

// index.ts
const app = express();
...
app.get("/path", doStuff())

I am able to spawn a fork of the file in a child process using a test file

// test.spec.ts
import { spawn, fork } from "child_process";

describe("do test",()=>{
  it("do stuff", async ()=>{
    const instance = spawn('ts-node', ['path/to/index.ts'],{
      cwd: 'path/to/workind/directory'});
  });
  console.log('#1',instance);
  await new Promise(r => setTimeout(r, 60000));
  console.log('#2',instance);
  ... // do stuff
  });
});

Now my issue is that I can't run all the tests as I only have a few seconds before the instance gets killed. In console log #1 everything looks fine but at #2 it's already destroyed after 60 seconds. When waiting to completely start up and running the tests I would need at least 15min of instance running without interruptions.

I tried it with fork instead of spawn already and plenty of spawn/fork parameters such as detached, silent and stdio.

Any idea what to do to keep it alive?

Thanks in advance :)

  • You're awaiting a promise but your test is not async! Try `it('should do stuff', async () => { ... })`. Though I think you'll get timeout error from jest (I'm assuming you're using jest) but it will say how to delay for a long running test in the error message. – h-sifat Mar 11 '22 at 00:37
  • Thx, I fixed it. Though this is not the real code, there I had it in place. I'm running ts-mocha but with the --timeout 0 flag therefore I don't get timeouts in my test, it's just the instance that isn't usable after one minute – Christian Buchwald Mar 11 '22 at 08:10

0 Answers0