Skip to content

Commit 939886c

Browse files
committed
Fix bun issue
1 parent 429c69b commit 939886c

File tree

5 files changed

+68
-20
lines changed

5 files changed

+68
-20
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"changes": { "package.json": "Patch" },
3+
"note": "Fix bun issue",
4+
"date": "2026-01-02T12:51:33.602062900Z"
5+
}

src/__tests__/__snapshots__/snapshot.test.tsx.snap

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
exports[`SnapshotTest React element snapshot 1`] = `
44
"<div>
5+
before
56
<div>
6-
before
7-
<div>
8-
SnapshotTestInner
9-
</div>
10-
after
7+
SnapshotTestInner
118
</div>
9+
after
1210
</div>"
1311
`;
1412

src/__tests__/dom.test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { describe, expect, test } from 'bun:test'
2+
import { createElement } from 'react'
3+
import { render } from '../index.ts'
4+
5+
function DomTestInner() {
6+
return createElement(
7+
'div',
8+
{
9+
className: 'dom-test-inner',
10+
},
11+
'DomTestInner',
12+
)
13+
}
14+
15+
function DomTest() {
16+
return createElement(
17+
'div',
18+
{
19+
className: 'dom-test',
20+
},
21+
'before',
22+
createElement(DomTestInner),
23+
'after',
24+
)
25+
}
26+
27+
describe('SnapshotTest', () => {
28+
test('HTML element snapshot with render', () => {
29+
const { container } = render(createElement(DomTest))
30+
expect(container.children[0]).toHaveClass('dom-test')
31+
})
32+
33+
test('HTML element snapshot with render', () => {
34+
expect(createElement(DomTest)).toHaveClass('dom-test')
35+
})
36+
})

src/__tests__/index.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ describe('expect with ReactElement', () => {
99
})
1010
})
1111

12-
describe('expect with HTMLElement', () => {
13-
test('should format HTMLElement', () => {
14-
const div = document.createElement('div')
15-
div.textContent = 'Hello DOM'
16-
// @ts-expect-error - mock transforms HTMLElement to string at runtime
17-
expect(div).toContain('Hello DOM')
18-
})
19-
})
20-
2112
describe('expect with primitive value', () => {
2213
test('should pass through primitive values', () => {
2314
expect('hello').toBe('hello')

src/index.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,33 @@ if (!GlobalRegistrator.isRegistered) {
1212
GlobalRegistrator.register()
1313
expect.extend(matchers)
1414

15+
const methods = ['toMatchSnapshot', 'toMatchInlineSnapshot', 'toContain']
16+
1517
const originalExpect = expect
1618
test.mock.module('bun:test', () => {
1719
const expect = (value: unknown) => {
18-
if (isReactElement(value)) {
19-
const { container } = render(value as ReactElement)
20-
return originalExpect(formatHTMLElement(container))
21-
}
22-
if (value instanceof HTMLElement) {
23-
return originalExpect(formatHTMLElement(value))
20+
if (value instanceof HTMLElement || isReactElement(value)) {
21+
const element =
22+
value instanceof HTMLElement
23+
? value
24+
: (render(value as ReactElement).container
25+
.children[0] as HTMLElement)
26+
const stringRet = originalExpect(formatHTMLElement(element))
27+
const jsonRet = originalExpect(element)
28+
for (const method of methods) {
29+
;(jsonRet as unknown as Record<string, unknown>)[method] = (
30+
...args: unknown[]
31+
) => {
32+
return (
33+
stringRet as unknown as Record<
34+
string,
35+
(...args: unknown[]) => unknown
36+
>
37+
)[method]?.(...(args as [object, string]))
38+
}
39+
}
40+
41+
return jsonRet
2442
}
2543
return originalExpect(value)
2644
}

0 commit comments

Comments
 (0)