41 lines
1.9 KiB
TypeScript
41 lines
1.9 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|||
|
|
import {
|
||
|
|
assertNoReactTestWarnings,
|
||
|
|
createReactWarningGuard,
|
||
|
|
formatReactTestWarnings,
|
||
|
|
isReactTestWarning,
|
||
|
|
} from './test-warning-guard'
|
||
|
|
|
||
|
|
describe('React test warning guard', () => {
|
||
|
|
it('recognizes React act and duplicate-key warnings only', () => {
|
||
|
|
expect(isReactTestWarning(['Warning: An update to Example inside a test was not wrapped in act(...).'])).toBe(true)
|
||
|
|
expect(isReactTestWarning(['Warning: Each child in a list should have a unique "key" prop.'])).toBe(true)
|
||
|
|
expect(isReactTestWarning(['Warning: Encountered two children with the same key, `duplicate`.'])).toBe(true)
|
||
|
|
expect(isReactTestWarning(['network request failed'])).toBe(false)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('collects target warnings for one compact diagnostic and forwards unrelated errors', () => {
|
||
|
|
const originalConsoleError = vi.fn()
|
||
|
|
const warnings: unknown[][] = []
|
||
|
|
const guardedConsoleError = createReactWarningGuard(originalConsoleError, warnings)
|
||
|
|
|
||
|
|
guardedConsoleError('Warning: An update to Example inside a test was not wrapped in act(...)')
|
||
|
|
guardedConsoleError('Warning: An update to Other inside a test was not wrapped in act(...)')
|
||
|
|
guardedConsoleError('Warning: Encountered two children with the same key, `duplicate`.')
|
||
|
|
guardedConsoleError('network request failed', { status: 500 })
|
||
|
|
|
||
|
|
expect(originalConsoleError).toHaveBeenCalledTimes(1)
|
||
|
|
expect(originalConsoleError).toHaveBeenCalledWith('network request failed', { status: 500 })
|
||
|
|
expect(formatReactTestWarnings(warnings)).toBe(
|
||
|
|
'React act warning: 2, React duplicate-key warning: 1 (Example, Other)',
|
||
|
|
)
|
||
|
|
expect(() => assertNoReactTestWarnings(warnings)).toThrow(
|
||
|
|
'React test warning guard: React act warning: 2, React duplicate-key warning: 1 (Example, Other).',
|
||
|
|
)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('passes only when no target warnings were captured', () => {
|
||
|
|
expect(() => assertNoReactTestWarnings([])).not.toThrow()
|
||
|
|
})
|
||
|
|
})
|