Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pretty-format): support react 19 #6909

Merged
merged 3 commits into from
Nov 18, 2024
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
3 changes: 2 additions & 1 deletion packages/pretty-format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"devDependencies": {
"@types/react-is": "^18.3.0",
"react-is": "^18.3.1"
"react-is": "^18.3.1",
"react-is-19": "npm:react-is@19.0.0-rc-b01722d5-20241114"
}
}
26 changes: 25 additions & 1 deletion packages/pretty-format/src/plugins/ReactElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,38 @@
*/

import type { Config, NewPlugin, Printer, Refs } from '../types'
import * as ReactIs from 'react-is'
import * as ReactIs18 from 'react-is'
// @ts-expect-error no type
import * as ReactIs19 from 'react-is-19'
import {
printChildren,
printElement,
printElementAsLeaf,
printProps,
} from './lib/markup'

const reactIsMethods = [
'isAsyncMode',
'isConcurrentMode',
'isContextConsumer',
'isContextProvider',
'isElement',
'isForwardRef',
'isFragment',
'isLazy',
'isMemo',
'isPortal',
'isProfiler',
'isStrictMode',
'isSuspense',
'isSuspenseList',
'isValidElementType',
] as const

const ReactIs: typeof ReactIs18 = Object.fromEntries(
reactIsMethods.map(m => [m, (v: any) => (ReactIs18 as any)[m](v) || ReactIs19[m](v)]),
) as any

// Given element.props.children, or subtree during recursive traversal,
// return flattened array of children.
function getChildren(arg: unknown, children: Array<unknown> = []) {
Expand Down
58 changes: 50 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"debug": "^4.3.4",
"immutable": "5.0.0-beta.5",
"memfs": "^4.8.2",
"react": "^18.3.1",
"react-19": "npm:react@19.0.0-rc-b01722d5-20241114",
"sweetalert2": "^11.6.16",
"tinyrainbow": "^1.2.0",
"tinyspy": "^1.0.2",
Expand Down
12 changes: 12 additions & 0 deletions test/core/test/snapshot-react-19.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @jsxRuntime automatic
// @jsxImportSource react-19

import { expect, test } from 'vitest'

test('react 19', () => {
expect(<div>hello</div>).toMatchInlineSnapshot(`
<div>
hello
</div>
`)
})
12 changes: 12 additions & 0 deletions test/core/test/snapshot-react.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @jsxRuntime automatic
// @jsxImportSource react

import { expect, test } from 'vitest'

test('react 18', () => {
expect(<div>hello</div>).toMatchInlineSnapshot(`
<div>
hello
</div>
`)
})