Play with time
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import { vi, describe, it, expect } from 'vitest';
|
||||
|
||||
vi.useFakeTimers();
|
||||
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
function delay(callback) {
|
||||
setTimeout(() => {
|
||||
@@ -9,5 +7,22 @@ function delay(callback) {
|
||||
}
|
||||
|
||||
describe('delay function', () => {
|
||||
it.todo('should call callback after delay', () => {});
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime('2024-02-29');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should call callback after delay', () => {
|
||||
const callback = vi.fn();
|
||||
|
||||
delay(callback);
|
||||
vi.advanceTimersToNextTimer();
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(new Date()).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user