简体   繁体   中英

How to mock the time for Later.JS

I am using Later.JS for a automatic function call every first day of the month. For testing I need to mock the time to check if Later.JS and my code does the right things. Can some one help me how I can mock this past time.

Thank you

What library do you use for unit testing? I like Jest because is very intuitive. It has function mock documented here .

jest.mock(path, moduleFactory) takes a module factory argument. A module factory is a function that returns the mock.

And usage:

jest.mock('laterJS', () => () => ({format: () => '2018–01–30T12:34:56+00:00'}));

Check also jest.spyOn documented here .

Last, but not least, there is sinonJS and its stubs , great tool for mocking functions and libraries and using your own implementation of function for testing purpose like returning hard-coded timestamp. Here it is .

EDIT: After editing question I understood what OP want to achieve

Alright, by unclear question I thought you were interesting in testing your code, now I'm aware you want to test both: laterJS schedule and your code.

Basically, all crones / schedulers are created for running code / tasks every some period of time. Under the hood, in very simplified way, scheduler is all the time checking if any of schedule conditions are met. If yes - do something. To achieve it you have mock clock to make laterJS to think that's a time to run his job.

I have no experience with laterJS, I have some with node-cron, but the idea is exactly the same. Check some of their test suites to realise what approach is proper one.

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