-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
66 lines (59 loc) · 2.08 KB
/
index.ts
File metadata and controls
66 lines (59 loc) · 2.08 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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 methods = ['toMatchSnapshot', 'toMatchInlineSnapshot', 'toContain']
const originalExpect = expect
test.mock.module('bun:test', () => {
const expect = (value: unknown) => {
if (value instanceof HTMLElement || isReactElement(value)) {
const element =
value instanceof HTMLElement
? value
: (render(value as ReactElement).container
.children[0] as HTMLElement)
const stringRet = originalExpect(formatHTMLElement(element))
const jsonRet = originalExpect(element)
for (const method of methods) {
;(jsonRet as unknown as Record<string, unknown>)[method] = (
...args: unknown[]
) => {
return (
stringRet as unknown as Record<
string,
(...args: unknown[]) => unknown
>
)[method]?.(...(args as [object, string]))
}
}
return jsonRet
}
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 { default as userEvent } from '@testing-library/user-event'