-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
48 lines (43 loc) · 1.47 KB
/
index.ts
File metadata and controls
48 lines (43 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import * as test from 'bun:test'
import { afterEach, expect } from 'bun:test'
import { GlobalRegistrator } from '@happy-dom/global-registrator'
import type { TestingLibraryMatchers } from '@testing-library/jest-dom/matchers'
import * as matchers from '@testing-library/jest-dom/matchers'
import { cleanup, render } from '@testing-library/react'
import type { ReactElement } from 'react'
import { formatHTMLElement } from './formatHTMLElement.ts'
import { isReactElement } from './isReactElement.ts'
if (!GlobalRegistrator.isRegistered) {
GlobalRegistrator.register()
expect.extend(matchers)
const originalExpect = expect
test.mock.module('bun:test', () => {
const expect = (value: unknown) => {
if (isReactElement(value)) {
const { container } = render(value as ReactElement)
return originalExpect(formatHTMLElement(container))
}
if (value instanceof HTMLElement) {
return originalExpect(formatHTMLElement(value))
}
return originalExpect(value)
}
Object.assign(expect, originalExpect)
return {
...test,
expect,
}
})
// Optional: cleans up `render` after each test
afterEach(() => {
cleanup()
})
}
declare module 'bun:test' {
interface Matchers<T>
extends TestingLibraryMatchers<typeof expect.stringContaining, T> {}
interface AsymmetricMatchers
extends TestingLibraryMatchers<unknown, unknown> {}
}
export * from '@testing-library/react'
export * from '@testing-library/user-event'