Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changepacks/changepack_log_1X3KB51ltpzMpxIMRC5gP.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"changes": { "package.json": "Patch" },
"note": "Fix bun issue",
"date": "2026-01-02T12:51:33.602062900Z"
}
8 changes: 3 additions & 5 deletions src/__tests__/__snapshots__/snapshot.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

exports[`SnapshotTest React element snapshot 1`] = `
"<div>
before
<div>
before
<div>
SnapshotTestInner
</div>
after
SnapshotTestInner
</div>
after
</div>"
`;

Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/dom.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, test } from 'bun:test'
import { createElement } from 'react'
import { render } from '../index.ts'

function DomTestInner() {
return createElement(
'div',
{
className: 'dom-test-inner',
},
'DomTestInner',
)
}

function DomTest() {
return createElement(
'div',
{
className: 'dom-test',
},
'before',
createElement(DomTestInner),
'after',
)
}

describe('SnapshotTest', () => {
test('HTML element snapshot with render', () => {
const { container } = render(createElement(DomTest))
expect(container.children[0]).toHaveClass('dom-test')
})

test('HTML element snapshot with render', () => {
expect(createElement(DomTest)).toHaveClass('dom-test')
})
})
9 changes: 0 additions & 9 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ describe('expect with ReactElement', () => {
})
})

describe('expect with HTMLElement', () => {
test('should format HTMLElement', () => {
const div = document.createElement('div')
div.textContent = 'Hello DOM'
// @ts-expect-error - mock transforms HTMLElement to string at runtime
expect(div).toContain('Hello DOM')
})
})

describe('expect with primitive value', () => {
test('should pass through primitive values', () => {
expect('hello').toBe('hello')
Expand Down
30 changes: 24 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,33 @@ 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 (isReactElement(value)) {
const { container } = render(value as ReactElement)
return originalExpect(formatHTMLElement(container))
}
if (value instanceof HTMLElement) {
return originalExpect(formatHTMLElement(value))
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)
}
Expand Down