简体   繁体   中英

Jest Unit Testing for Cron

 //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() }

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

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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM