Skip to content

Commit 2699e51

Browse files
committed
test: テストの後片付けを強化 (CodeRabbit 指摘対応)
FadeIn / SlideIn / useReducedMotion テストで window.matchMedia と fake timer の状態が他テストにリークしていた問題を修正: - afterEach で originalMatchMedia へ復元 - vi.runOnlyPendingTimers() → vi.useRealTimers() の順で fake timer をクリア - vi.restoreAllMocks() でモックを完全リセット
1 parent 638ca74 commit 2699e51

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/components/animations/FadeIn.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
22
import { render, screen, act } from '@testing-library/react';
33
import { FadeIn } from './FadeIn';
44

5+
const originalMatchMedia = window.matchMedia;
6+
57
const mockMatchMedia = (matches: boolean) => {
68
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
79
matches,
@@ -22,7 +24,10 @@ describe('FadeIn', () => {
2224
});
2325

2426
afterEach(() => {
27+
vi.runOnlyPendingTimers();
2528
vi.useRealTimers();
29+
window.matchMedia = originalMatchMedia;
30+
vi.restoreAllMocks();
2631
});
2732

2833
it('renders children', () => {

src/components/animations/SlideIn.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
22
import { render, screen, act } from '@testing-library/react';
33
import { SlideIn } from './SlideIn';
44

5+
const originalMatchMedia = window.matchMedia;
6+
57
const mockMatchMedia = (matches: boolean) => {
68
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
79
matches,
@@ -22,7 +24,10 @@ describe('SlideIn', () => {
2224
});
2325

2426
afterEach(() => {
27+
vi.runOnlyPendingTimers();
2528
vi.useRealTimers();
29+
window.matchMedia = originalMatchMedia;
30+
vi.restoreAllMocks();
2631
});
2732

2833
it('renders children', () => {

src/hooks/useReducedMotion.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { describe, it, expect, vi, beforeEach } from 'vitest';
1+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
22
import { renderHook } from '@testing-library/react';
33
import { useReducedMotion } from './useReducedMotion';
44

5+
const originalMatchMedia = window.matchMedia;
6+
57
const createMock = (matches: boolean) => ({
68
matches,
79
media: '(prefers-reduced-motion: reduce)',
@@ -18,6 +20,11 @@ describe('useReducedMotion', () => {
1820
window.matchMedia = vi.fn(() => createMock(false)) as unknown as typeof window.matchMedia;
1921
});
2022

23+
afterEach(() => {
24+
window.matchMedia = originalMatchMedia;
25+
vi.restoreAllMocks();
26+
});
27+
2128
it('returns false when prefers-reduced-motion is not set', () => {
2229
const { result } = renderHook(() => useReducedMotion());
2330
expect(result.current).toBe(false);

0 commit comments

Comments
 (0)