From c6b550964d34bca44f899cd59180cb4247aff5ed Mon Sep 17 00:00:00 2001 From: minho jang Date: Sun, 24 Aug 2025 01:07:26 +0900 Subject: [PATCH] test(compat/defer): use fake timer instead of done() callback - Vitest says `done()` callback is deprecated, so I replaced it with fake timer. --- src/compat/function/defer.spec.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/compat/function/defer.spec.ts b/src/compat/function/defer.spec.ts index cb95ff5b1..391d42f72 100644 --- a/src/compat/function/defer.spec.ts +++ b/src/compat/function/defer.spec.ts @@ -1,10 +1,18 @@ -import { describe, expect, expectTypeOf, it } from 'vitest'; +import { afterEach, beforeEach, describe, expect, expectTypeOf, it, vi } from 'vitest'; import type { defer as deferLodash } from 'lodash'; import { defer } from './defer'; describe('defer', () => { - it('should provide additional arguments to `func`', (done: () => void) => { - let args: any[]; + beforeEach(() => { + vi.useFakeTimers(); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it('should provide additional arguments to `func`', () => { + let args: any[] = []; defer( // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -16,13 +24,11 @@ describe('defer', () => { 2 ); - setTimeout(() => { - expect(args).toEqual([1, 2]); - done(); - }, 32); + vi.advanceTimersByTime(32); + expect(args).toEqual([1, 2]); }); - it('should be cancelable', (done: () => void) => { + it('should be cancelable', () => { let pass = true; const timerId = defer(() => { pass = false; @@ -30,10 +36,8 @@ describe('defer', () => { clearTimeout(timerId); - setTimeout(() => { - expect(pass); - done(); - }, 32); + vi.advanceTimersByTime(32); + expect(pass).toBe(true); }); it('should throw an error if `func` is not a function', () => {