Play with time

This commit is contained in:
Steve Kinney
2024-10-02 16:26:35 -05:00
parent 8fb10ac976
commit a9e36ee3c8
6 changed files with 69 additions and 15 deletions

View File

@@ -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);
});
});