Nestjs dynamic cron scheduling testing with jest

Viewed 277

I am trying to include nestjs' dynamic cron scheduling feature as documented here: https://docs.nestjs.com/techniques/task-scheduling

When running a unit test with jest, i am facing this issue: A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.

After narrowing it down, the issue points to job.start(). Since this is an 'open' job, how do i close it in jest to resolve the error above?

Any help would be much appreciated!

addCronJob(name: string, seconds: string) {
  const job = new CronJob(`${seconds} * * * * *`, () => {
    this.logger.warn(`time (${seconds}) for job ${name} to run!`);
  });

  this.schedulerRegistry.addCronJob(name, job);
  job.start();

  this.logger.warn(
    `job ${name} added for each minute at ${seconds} seconds!`,
  );
}
0 Answers
Related