Home > Mobile >  Jest Unit Testing for Cron
Jest Unit Testing for Cron

Time:11-24

//How to write Unit Test Case for the following Cron Job

index.js

import cron from 'node-cron'
export const cronJob = cron.schedule(pattern , async()=> {
  // do something
}

index.spec.js

import { cron } from 'node-cron';
import {cronJob} from '.'
jest.mock('node-cron', () => {
    return {
      schedule: jest.fn(),
    };
 });
 describe("Run Cron" , async()=> {
    await cronJob.start()
    await cronJob.destroy()
 }
 
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

This above code is not getting covered by TC Do need a help thank you The TC is covered 0% of the Code i dont know why

CodePudding user response:

I would suggest testing the job function itself and not the scheduling, since that's where your "business logic" will live. And that way you don't need to mock node-cron.

CodePudding user response:

You should NOT test the library itself, somebody has already done it for(most likely). But you DO need to test your own code, which is the function you pass to cron.schedule :)

  • Related