Skip to content

Commit 793988e

Browse files
committed
fix: compatibility with react 19 in snapshots
1 parent fd3d6cf commit 793988e

File tree

4 files changed

+174
-10
lines changed

4 files changed

+174
-10
lines changed

packages/pretty-format/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
"dependencies": {
2424
"@jest/schemas": "workspace:*",
2525
"ansi-styles": "^5.2.0",
26-
"react-is": "^18.3.1"
26+
"react-is": "^19.1.0"
2727
},
2828
"devDependencies": {
2929
"@types/react": "^18.3.23",
30-
"@types/react-is": "^18.3.1",
30+
"@types/react-is": "^19.0.0",
3131
"@types/react-test-renderer": "^18.3.1",
3232
"immutable": "^5.1.2",
3333
"jest-util": "workspace:*",

packages/pretty-format/src/plugins/ReactElement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import * as ReactIs from 'react-is';
98
import type {Config, NewPlugin, Printer, Refs} from '../types';
9+
import * as ReactIs from './lib/ReactIs';
1010
import {
1111
printChildren,
1212
printElement,
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2+
// This file is mostly copy-pasted from the original ReactIs library,
3+
// with some modifications to support React versions < 19.
4+
// https://github.com/facebook/react/blob/main/packages/react-is/src/ReactIs.js
5+
6+
// Customizations:
7+
// - Added support for REACT_LEGACY_ELEMENT_TYPE wherever REACT_ELEMENT_TYPE is used.
8+
9+
// Types collected from https://github.com/facebook/react/blob/main/packages/shared/ReactSymbols.js
10+
const REACT_LEGACY_ELEMENT_TYPE: symbol = Symbol.for('react.element');
11+
const REACT_ELEMENT_TYPE: symbol = Symbol.for('react.transitional.element');
12+
const REACT_PORTAL_TYPE: symbol = Symbol.for('react.portal');
13+
const REACT_FRAGMENT_TYPE: symbol = Symbol.for('react.fragment');
14+
const REACT_STRICT_MODE_TYPE: symbol = Symbol.for('react.strict_mode');
15+
const REACT_PROFILER_TYPE: symbol = Symbol.for('react.profiler');
16+
const REACT_CONSUMER_TYPE: symbol = Symbol.for('react.consumer');
17+
const REACT_CONTEXT_TYPE: symbol = Symbol.for('react.context');
18+
const REACT_FORWARD_REF_TYPE: symbol = Symbol.for('react.forward_ref');
19+
const REACT_SUSPENSE_TYPE: symbol = Symbol.for('react.suspense');
20+
const REACT_SUSPENSE_LIST_TYPE: symbol = Symbol.for('react.suspense_list');
21+
const REACT_MEMO_TYPE: symbol = Symbol.for('react.memo');
22+
const REACT_LAZY_TYPE: symbol = Symbol.for('react.lazy');
23+
const REACT_VIEW_TRANSITION_TYPE: symbol = Symbol.for('react.view_transition');
24+
const REACT_CLIENT_REFERENCE: symbol = Symbol.for('react.client.reference');
25+
26+
// Below is a copy of ReactIs.js with customizations listed above.
27+
28+
export function typeOf(object: any): any {
29+
if (typeof object === 'object' && object !== null) {
30+
const $$typeof = object.$$typeof;
31+
switch ($$typeof) {
32+
case REACT_LEGACY_ELEMENT_TYPE:
33+
case REACT_ELEMENT_TYPE:
34+
const type = object.type;
35+
36+
switch (type) {
37+
case REACT_FRAGMENT_TYPE:
38+
case REACT_PROFILER_TYPE:
39+
case REACT_STRICT_MODE_TYPE:
40+
case REACT_SUSPENSE_TYPE:
41+
case REACT_SUSPENSE_LIST_TYPE:
42+
case REACT_VIEW_TRANSITION_TYPE:
43+
return type;
44+
default:
45+
const $$typeofType = type && type.$$typeof;
46+
47+
switch ($$typeofType) {
48+
case REACT_CONTEXT_TYPE:
49+
case REACT_FORWARD_REF_TYPE:
50+
case REACT_LAZY_TYPE:
51+
case REACT_MEMO_TYPE:
52+
return $$typeofType;
53+
case REACT_CONSUMER_TYPE:
54+
return $$typeofType;
55+
// Fall through
56+
default:
57+
return $$typeof;
58+
}
59+
}
60+
case REACT_PORTAL_TYPE:
61+
return $$typeof;
62+
}
63+
}
64+
65+
return undefined;
66+
}
67+
68+
export const ContextConsumer: symbol = REACT_CONSUMER_TYPE;
69+
export const ContextProvider: symbol = REACT_CONTEXT_TYPE;
70+
export const Element = REACT_ELEMENT_TYPE;
71+
export const ForwardRef = REACT_FORWARD_REF_TYPE;
72+
export const Fragment = REACT_FRAGMENT_TYPE;
73+
export const Lazy = REACT_LAZY_TYPE;
74+
export const Memo = REACT_MEMO_TYPE;
75+
export const Portal = REACT_PORTAL_TYPE;
76+
export const Profiler = REACT_PROFILER_TYPE;
77+
export const StrictMode = REACT_STRICT_MODE_TYPE;
78+
export const Suspense = REACT_SUSPENSE_TYPE;
79+
export const SuspenseList = REACT_SUSPENSE_LIST_TYPE;
80+
81+
export function isValidElementType(type: any): boolean {
82+
if (typeof type === 'string' || typeof type === 'function') {
83+
return true;
84+
}
85+
86+
// Note: typeof might be other than 'symbol' or 'number' (e.g. if it's a polyfill).
87+
if (
88+
type === REACT_FRAGMENT_TYPE ||
89+
type === REACT_PROFILER_TYPE ||
90+
type === REACT_STRICT_MODE_TYPE ||
91+
type === REACT_SUSPENSE_TYPE ||
92+
type === REACT_SUSPENSE_LIST_TYPE
93+
) {
94+
return true;
95+
}
96+
97+
if (typeof type === 'object' && type !== null) {
98+
if (
99+
type.$$typeof === REACT_LAZY_TYPE ||
100+
type.$$typeof === REACT_MEMO_TYPE ||
101+
type.$$typeof === REACT_CONTEXT_TYPE ||
102+
type.$$typeof === REACT_CONSUMER_TYPE ||
103+
type.$$typeof === REACT_FORWARD_REF_TYPE ||
104+
// This needs to include all possible module reference object
105+
// types supported by any Flight configuration anywhere since
106+
// we don't know which Flight build this will end up being used
107+
// with.
108+
type.$$typeof === REACT_CLIENT_REFERENCE ||
109+
type.getModuleId !== undefined
110+
) {
111+
return true;
112+
}
113+
}
114+
115+
return false;
116+
}
117+
118+
export function isContextConsumer(object: any): boolean {
119+
return typeOf(object) === REACT_CONSUMER_TYPE;
120+
}
121+
export function isContextProvider(object: any): boolean {
122+
return typeOf(object) === REACT_CONTEXT_TYPE;
123+
}
124+
export function isElement(object: any): boolean {
125+
return (
126+
typeof object === 'object' &&
127+
object !== null &&
128+
[REACT_ELEMENT_TYPE, REACT_LEGACY_ELEMENT_TYPE].includes(object.$$typeof)
129+
);
130+
}
131+
export function isForwardRef(object: any): boolean {
132+
return typeOf(object) === REACT_FORWARD_REF_TYPE;
133+
}
134+
export function isFragment(object: any): boolean {
135+
return typeOf(object) === REACT_FRAGMENT_TYPE;
136+
}
137+
export function isLazy(object: any): boolean {
138+
return typeOf(object) === REACT_LAZY_TYPE;
139+
}
140+
export function isMemo(object: any): boolean {
141+
return typeOf(object) === REACT_MEMO_TYPE;
142+
}
143+
export function isPortal(object: any): boolean {
144+
return typeOf(object) === REACT_PORTAL_TYPE;
145+
}
146+
export function isProfiler(object: any): boolean {
147+
return typeOf(object) === REACT_PROFILER_TYPE;
148+
}
149+
export function isStrictMode(object: any): boolean {
150+
return typeOf(object) === REACT_STRICT_MODE_TYPE;
151+
}
152+
export function isSuspense(object: any): boolean {
153+
return typeOf(object) === REACT_SUSPENSE_TYPE;
154+
}
155+
export function isSuspenseList(object: any): boolean {
156+
return typeOf(object) === REACT_SUSPENSE_LIST_TYPE;
157+
}

yarn.lock

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6390,12 +6390,12 @@ __metadata:
63906390
languageName: node
63916391
linkType: hard
63926392

6393-
"@types/react-is@npm:^18.3.1":
6394-
version: 18.3.1
6395-
resolution: "@types/react-is@npm:18.3.1"
6393+
"@types/react-is@npm:^19.0.0":
6394+
version: 19.0.0
6395+
resolution: "@types/react-is@npm:19.0.0"
63966396
dependencies:
6397-
"@types/react": "npm:^18"
6398-
checksum: 10/ccb79d6e196a5232cde8ccb255ec97e062801a3dafeff3816130fb5ad6b9a87f7c0806ab35bc00890a229773228ef217d0390839b68c705d3add2f798b5fcf82
6397+
"@types/react": "npm:*"
6398+
checksum: 10/c56e39e88558a71bc0b1deaa84cce9d5c84d6522057b73690d099b0763898203ec29a91563431e83b5086c49ac64a239fe993c50a4f008d6fbfc551587db5895
63996399
languageName: node
64006400
linkType: hard
64016401

@@ -18234,14 +18234,14 @@ __metadata:
1823418234
dependencies:
1823518235
"@jest/schemas": "workspace:*"
1823618236
"@types/react": "npm:^18.3.23"
18237-
"@types/react-is": "npm:^18.3.1"
18237+
"@types/react-is": "npm:^19.0.0"
1823818238
"@types/react-test-renderer": "npm:^18.3.1"
1823918239
ansi-styles: "npm:^5.2.0"
1824018240
immutable: "npm:^5.1.2"
1824118241
jest-util: "workspace:*"
1824218242
react: "npm:18.3.1"
1824318243
react-dom: "npm:18.3.1"
18244-
react-is: "npm:^18.3.1"
18244+
react-is: "npm:^19.1.0"
1824518245
react-test-renderer: "npm:18.3.1"
1824618246
languageName: unknown
1824718247
linkType: soft
@@ -18637,6 +18637,13 @@ __metadata:
1863718637
languageName: node
1863818638
linkType: hard
1863918639

18640+
"react-is@npm:^19.1.0":
18641+
version: 19.1.0
18642+
resolution: "react-is@npm:19.1.0"
18643+
checksum: 10/4aceca94492032a19411138bf68c6ca7dce4abee7ae6c7f23db9291d2dff14d6a643717b176ef9a151e8cdf6a2b71fdbc59fd998328e086a6f752512304a252b
18644+
languageName: node
18645+
linkType: hard
18646+
1864018647
"react-json-view-lite@npm:^1.2.0":
1864118648
version: 1.5.0
1864218649
resolution: "react-json-view-lite@npm:1.5.0"

0 commit comments

Comments
 (0)