From edd468cf1d29f88dc733867bdb1ca7ea9cb95a39 Mon Sep 17 00:00:00 2001 From: alisaza Date: Sun, 13 Sep 2026 13:42:22 +0330 Subject: [PATCH] test: enhance test configurations and cleanup procedures Updated the Vitest configuration to increase test and hook timeouts to 20 seconds, addressing flaky tests under pre-push load. Additionally, improved test cleanup procedures across multiple test files by ensuring mocks are cleared after each test, enhancing test reliability and maintainability. --- context/AlertModalContext.test.tsx | 30 ++++++++++++++++++- .../detail/EventDiscountsPanel.test.tsx | 8 ++++- vitest.config.ts | 5 ++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/context/AlertModalContext.test.tsx b/context/AlertModalContext.test.tsx index f5ce0c6..dadb7ea 100644 --- a/context/AlertModalContext.test.tsx +++ b/context/AlertModalContext.test.tsx @@ -1,10 +1,38 @@ +import type { ButtonHTMLAttributes } from 'react' + import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' import { AlertModalProvider } from '@/context/AlertModalContext' import useAlertModal from '@/hooks/useAlertModal' -afterEach(cleanup) +vi.mock('@/components/formElements/Button', () => ({ + default: ({ + children, + isLoading, + fullWidth: _fullWidth, + color: _color, + variant: _variant, + ...props + }: ButtonHTMLAttributes & { + isLoading?: boolean + fullWidth?: boolean + color?: string + variant?: string + }) => ( + + ), +})) + +afterEach(() => { + cleanup() + vi.clearAllMocks() +}) function AlertHarness({ onConfirm }: { onConfirm: () => void | Promise }) { const { showAlert } = useAlertModal() diff --git a/features/events/detail/EventDiscountsPanel.test.tsx b/features/events/detail/EventDiscountsPanel.test.tsx index d90c1e3..69e143c 100644 --- a/features/events/detail/EventDiscountsPanel.test.tsx +++ b/features/events/detail/EventDiscountsPanel.test.tsx @@ -71,7 +71,10 @@ const freshCode = { } describe('EventDiscountsPanel', () => { - afterEach(cleanup) + afterEach(() => { + cleanup() + vi.clearAllMocks() + }) beforeEach(() => { vi.clearAllMocks() @@ -143,6 +146,7 @@ describe('EventDiscountsPanel', () => { ) await screen.findByText('WELCOME20') + bulkCreate.mockClear() fireEvent.change(screen.getByLabelText('درصد تخفیف (۱ تا ۹۹)'), { target: { value: '25' } }) fireEvent.change(screen.getByLabelText('تعداد کد یکتا'), { target: { value: '2' } }) @@ -164,6 +168,8 @@ describe('EventDiscountsPanel', () => { ) await screen.findByText('WELCOME20') + bulkCreate.mockClear() + addToast.mockClear() fireEvent.change(screen.getByLabelText('درصد تخفیف (۱ تا ۹۹)'), { target: { value: '150' } }) fireEvent.click(screen.getByRole('button', { name: 'ساخت کد تخفیف' })) diff --git a/vitest.config.ts b/vitest.config.ts index 56128cb..078aff0 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -12,6 +12,11 @@ export default defineConfig({ environment: 'jsdom', include: ['**/*.test.{ts,tsx}'], setupFiles: ['./vitest.setup.ts'], + // Default 5s flakes under pre-push load (jsdom + HeroUI transform); aborted + // tests then leak async work into the next case in the same file. + testTimeout: 20_000, + hookTimeout: 20_000, + maxWorkers: '50%', coverage: { provider: 'v8', include: ['lib/authRouting.ts', 'helpers/listResponse.ts', 'validation/auth.ts'],