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.
This commit is contained in:
parent
3f072976fe
commit
edd468cf1d
@ -1,10 +1,38 @@
|
|||||||
|
import type { ButtonHTMLAttributes } from 'react'
|
||||||
|
|
||||||
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
|
||||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||||
|
|
||||||
import { AlertModalProvider } from '@/context/AlertModalContext'
|
import { AlertModalProvider } from '@/context/AlertModalContext'
|
||||||
import useAlertModal from '@/hooks/useAlertModal'
|
import useAlertModal from '@/hooks/useAlertModal'
|
||||||
|
|
||||||
afterEach(cleanup)
|
vi.mock('@/components/formElements/Button', () => ({
|
||||||
|
default: ({
|
||||||
|
children,
|
||||||
|
isLoading,
|
||||||
|
fullWidth: _fullWidth,
|
||||||
|
color: _color,
|
||||||
|
variant: _variant,
|
||||||
|
...props
|
||||||
|
}: ButtonHTMLAttributes<HTMLButtonElement> & {
|
||||||
|
isLoading?: boolean
|
||||||
|
fullWidth?: boolean
|
||||||
|
color?: string
|
||||||
|
variant?: string
|
||||||
|
}) => (
|
||||||
|
<button
|
||||||
|
{...props}
|
||||||
|
disabled={props.disabled || isLoading}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</button>
|
||||||
|
),
|
||||||
|
}))
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
cleanup()
|
||||||
|
vi.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
function AlertHarness({ onConfirm }: { onConfirm: () => void | Promise<void> }) {
|
function AlertHarness({ onConfirm }: { onConfirm: () => void | Promise<void> }) {
|
||||||
const { showAlert } = useAlertModal()
|
const { showAlert } = useAlertModal()
|
||||||
|
|||||||
@ -71,7 +71,10 @@ const freshCode = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('EventDiscountsPanel', () => {
|
describe('EventDiscountsPanel', () => {
|
||||||
afterEach(cleanup)
|
afterEach(() => {
|
||||||
|
cleanup()
|
||||||
|
vi.clearAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
@ -143,6 +146,7 @@ describe('EventDiscountsPanel', () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
await screen.findByText('WELCOME20')
|
await screen.findByText('WELCOME20')
|
||||||
|
bulkCreate.mockClear()
|
||||||
|
|
||||||
fireEvent.change(screen.getByLabelText('درصد تخفیف (۱ تا ۹۹)'), { target: { value: '25' } })
|
fireEvent.change(screen.getByLabelText('درصد تخفیف (۱ تا ۹۹)'), { target: { value: '25' } })
|
||||||
fireEvent.change(screen.getByLabelText('تعداد کد یکتا'), { target: { value: '2' } })
|
fireEvent.change(screen.getByLabelText('تعداد کد یکتا'), { target: { value: '2' } })
|
||||||
@ -164,6 +168,8 @@ describe('EventDiscountsPanel', () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
await screen.findByText('WELCOME20')
|
await screen.findByText('WELCOME20')
|
||||||
|
bulkCreate.mockClear()
|
||||||
|
addToast.mockClear()
|
||||||
|
|
||||||
fireEvent.change(screen.getByLabelText('درصد تخفیف (۱ تا ۹۹)'), { target: { value: '150' } })
|
fireEvent.change(screen.getByLabelText('درصد تخفیف (۱ تا ۹۹)'), { target: { value: '150' } })
|
||||||
fireEvent.click(screen.getByRole('button', { name: 'ساخت کد تخفیف' }))
|
fireEvent.click(screen.getByRole('button', { name: 'ساخت کد تخفیف' }))
|
||||||
|
|||||||
@ -12,6 +12,11 @@ export default defineConfig({
|
|||||||
environment: 'jsdom',
|
environment: 'jsdom',
|
||||||
include: ['**/*.test.{ts,tsx}'],
|
include: ['**/*.test.{ts,tsx}'],
|
||||||
setupFiles: ['./vitest.setup.ts'],
|
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: {
|
coverage: {
|
||||||
provider: 'v8',
|
provider: 'v8',
|
||||||
include: ['lib/authRouting.ts', 'helpers/listResponse.ts', 'validation/auth.ts'],
|
include: ['lib/authRouting.ts', 'helpers/listResponse.ts', 'validation/auth.ts'],
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user