}
return pure.renderHook(hook, {...options})
}
export function testBaseElement() {
const {baseElement: baseDefaultElement} = render()
expectType(baseDefaultElement)
const container = document.createElement('input')
const {baseElement: baseElementFromContainer} = render(, {container})
expectType(
baseElementFromContainer,
)
const baseElementOption = document.createElement('input')
const {baseElement: baseElementFromOption} = render(, {
baseElement: baseElementOption,
})
expectType(
baseElementFromOption,
)
}
export function testRenderHook() {
const {result, rerender, unmount} = renderHook(() => React.useState(2)[0])
expectType(result.current)
rerender()
unmount()
renderHook(() => null, {wrapper: () => null})
}
export function testRenderHookProps() {
const {result, rerender, unmount} = renderHook(
({defaultValue}) => React.useState(defaultValue)[0],
{initialProps: {defaultValue: 2}},
)
expectType(result.current)
rerender()
unmount()
}
export function testContainer() {
render('a', {container: document.createElement('div')})
render('a', {container: document.createDocumentFragment()})
// Only allowed in React 19
render('a', {container: document})
render('a', {container: document.createElement('div'), hydrate: true})
// Only allowed for createRoot but typing `render` appropriately makes it harder to compose.
render('a', {container: document.createDocumentFragment(), hydrate: true})
render('a', {container: document, hydrate: true})
renderHook(() => null, {container: document.createElement('div')})
renderHook(() => null, {container: document.createDocumentFragment()})
// Only allowed in React 19
renderHook(() => null, {container: document})
renderHook(() => null, {
container: document.createElement('div'),
hydrate: true,
})
// Only allowed for createRoot but typing `render` appropriately makes it harder to compose.
renderHook(() => null, {
container: document.createDocumentFragment(),
hydrate: true,
})
renderHook(() => null, {container: document, hydrate: true})
}
export function testErrorHandlers() {
// This test runs under both React 18 and React 19 types. For React 19, verify that this works with `"@types/react": "npm:types-react@rc"`.
render(null, {
// This option is intended to type-check successfully with React 19 types
// but to fail (produce a type error) with React 18 types.
//
// > Why use `@ts-ignore` instead of `@ts-expect-error`?
// > Semantically, `@ts-expect-error` would be the right choice here:
// > it would fail under React 18 types and be unused under React 19 types.
// > However, when running with React 19 types, TypeScript reports that
// > `@ts-expect-error` is unused, which causes unwanted noise in CI.
// > To avoid those unused-directive diagnostics while still supporting
// > both React 18 and React 19 typings, we pragmatically use `@ts-ignore`,
// > acknowledging that it suppresses any real type errors at this site.
//
// eslint-disable-next-line @typescript-eslint/ban-ts-comment, @typescript-eslint/prefer-ts-expect-error
// @ts-ignore
onCaughtError: () => {},
})
render(null, {
// Should never work as it's not supported yet.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
onUncaughtError: () => {},
})
render(null, {
onRecoverableError: (error, errorInfo) => {
console.error(error)
console.log(errorInfo.componentStack)
},
})
}
/*
eslint
testing-library/prefer-explicit-assert: "off",
testing-library/no-wait-for-empty-callback: "off",
testing-library/prefer-screen-queries: "off"
*/
// https://stackoverflow.com/questions/53807517/how-to-test-if-two-types-are-exactly-the-same
type IfEquals = (() => G extends T
? 1
: 2) extends () => G extends U ? 1 : 2
? Yes
: No
/**
* Issues a type error if `Expected` is not identical to `Actual`.
*
* `Expected` should be declared when invoking `expectType`.
* `Actual` should almost always we be a `typeof value` statement.
*
* Source: https://github.com/mui-org/material-ui/blob/6221876a4b468a3330ffaafa8472de7613933b87/packages/material-ui-types/index.d.ts#L73-L84
*
* @example `expectType(value)`
* TypeScript issues a type error since `value is not assignable to never`.
* This means `typeof value` is not identical to `number | string`
* @param actual
*/
declare function expectType(
actual: IfEquals,
): void