Skip to content

Commit 37ec893

Browse files
committed
fix: usage of vi.useFakeTimers in test after updating to vitest 3
1 parent f8479fd commit 37ec893

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

src/index.spec.ts

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Component, Injectable, inject } from '@angular/core';
33
import { TestBed } from '@angular/core/testing';
44
import { BehaviorSubject, from } from 'rxjs';
55
import { writable as svelteWritable } from 'svelte/store';
6-
import { afterEach, describe, expect, it, vi } from 'vitest';
6+
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest';
77
import type {
88
OnUseArgument,
99
Readable,
@@ -2112,40 +2112,47 @@ describe('stores', () => {
21122112
unsubscribe();
21132113
});
21142114

2115-
it('should work with DebounceStore example', () => {
2116-
class DebounceStore<T> extends DerivedStore<T, SubscribableStore<T>> {
2117-
constructor(
2118-
store: SubscribableStore<T>,
2119-
initialValue: T,
2120-
private _delay: number
2121-
) {
2122-
super(store, initialValue);
2123-
}
2124-
protected derive(value: T) {
2125-
const timeout = setTimeout(() => this.set(value), this._delay);
2126-
return () => clearTimeout(timeout);
2115+
describe('with fake timers', () => {
2116+
beforeAll(() => {
2117+
vi.useFakeTimers();
2118+
});
2119+
afterAll(() => {
2120+
vi.useRealTimers();
2121+
});
2122+
it('should work with DebounceStore example', () => {
2123+
class DebounceStore<T> extends DerivedStore<T, SubscribableStore<T>> {
2124+
constructor(
2125+
store: SubscribableStore<T>,
2126+
initialValue: T,
2127+
private _delay: number
2128+
) {
2129+
super(store, initialValue);
2130+
}
2131+
protected derive(value: T) {
2132+
const timeout = setTimeout(() => this.set(value), this._delay);
2133+
return () => clearTimeout(timeout);
2134+
}
21272135
}
2128-
}
2129-
vi.useFakeTimers();
2130-
const a = writable(1);
2131-
const b = new DebounceStore(a, 0, 100);
2132-
const values: number[] = [];
2133-
const unsubscribe = b.subscribe((value) => values.push(value));
2134-
expect(values).toEqual([0]);
2135-
vi.advanceTimersByTime(99);
2136-
expect(values).toEqual([0]);
2137-
vi.advanceTimersByTime(1);
2138-
expect(values).toEqual([0, 1]);
2139-
a.set(2);
2140-
vi.advanceTimersByTime(1);
2141-
a.set(3);
2142-
vi.advanceTimersByTime(99);
2143-
expect(values).toEqual([0, 1]);
2144-
vi.advanceTimersByTime(1);
2145-
expect(values).toEqual([0, 1, 3]);
2146-
unsubscribe();
2147-
});
21482136

2137+
const a = writable(1);
2138+
const b = new DebounceStore(a, 0, 100);
2139+
const values: number[] = [];
2140+
const unsubscribe = b.subscribe((value) => values.push(value));
2141+
expect(values).toEqual([0]);
2142+
vi.advanceTimersByTime(99);
2143+
expect(values).toEqual([0]);
2144+
vi.advanceTimersByTime(1);
2145+
expect(values).toEqual([0, 1]);
2146+
a.set(2);
2147+
vi.advanceTimersByTime(1);
2148+
a.set(3);
2149+
vi.advanceTimersByTime(99);
2150+
expect(values).toEqual([0, 1]);
2151+
vi.advanceTimersByTime(1);
2152+
expect(values).toEqual([0, 1, 3]);
2153+
unsubscribe();
2154+
});
2155+
});
21492156
it('should infer types automatically in the async case', () => {
21502157
const a = writable(1);
21512158
const b = writable(2);

0 commit comments

Comments
 (0)