1
+/**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @emails react-core
8
+ */
9
+
10
+'use strict';
11
+
12
+describe('ReactMismatchedVersions-test', () => {
13
+ // Polyfills for test environment
14
+ global.ReadableStream =
15
+ require('web-streams-polyfill/ponyfill/es6').ReadableStream;
16
+ global.TextEncoder = require('util').TextEncoder;
17
+
18
+ let React;
19
+ let actualReactVersion;
20
+
21
+ beforeEach(() => {
22
+ jest.resetModules();
23
+ jest.mock('react', () => {
24
+ const actualReact = jest.requireActual('react');
25
+ return {
26
+ ...actualReact,
27
+ version: '18.0.0-whoa-this-aint-the-right-react',
28
+ __actualVersion: actualReact.version,
29
+ };
30
+ });
31
+ React = require('react');
32
+ actualReactVersion = React.__actualVersion;
33
+ });
34
+
35
+ test('importing "react-dom/client" throws if version does not match React version', async () => {
36
+ expect(() => require('react-dom/client')).toThrow(
37
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
38
+ 'must have the exact same version. Instead got:\n' +
39
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
40
+ ` - react-dom: ${actualReactVersion}`,
41
+ );
42
+ });
43
+
44
+ // When running in source mode, we lazily require the implementation to
45
+ // simulate the static config dependency injection we do at build time. So it
46
+ // only errors once you call something and trigger the require. Running the
47
+ // test in build mode is sufficient.
48
+ // @gate !source
49
+ test('importing "react-dom/server" throws if version does not match React version', async () => {
50
+ expect(() => require('react-dom/server')).toThrow(
51
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
52
+ 'must have the exact same version. Instead got:\n' +
53
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
54
+ ` - react-dom: ${actualReactVersion}`,
55
+ );
56
+ });
57
+
58
+ // @gate !source
59
+ test('importing "react-dom/server.node" throws if version does not match React version', async () => {
60
+ expect(() => require('react-dom/server.node')).toThrow(
61
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
62
+ 'must have the exact same version. Instead got:\n' +
63
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
64
+ ` - react-dom: ${actualReactVersion}`,
65
+ );
66
+ });
67
+
68
+ // @gate !source
69
+ test('importing "react-dom/server.browser" throws if version does not match React version', async () => {
70
+ expect(() => require('react-dom/server.browser')).toThrow(
71
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
72
+ 'must have the exact same version. Instead got:\n' +
73
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
74
+ ` - react-dom: ${actualReactVersion}`,
75
+ );
76
+ });
77
+
78
+ // @gate !source
79
+ test('importing "react-dom/server.bun" throws if version does not match React version', async () => {
80
+ expect(() => require('react-dom/server.bun')).toThrow(
81
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
82
+ 'must have the exact same version. Instead got:\n' +
83
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
84
+ ` - react-dom: ${actualReactVersion}`,
85
+ );
86
+ });
87
+
88
+ // @gate !source
89
+ test('importing "react-dom/server.edge" throws if version does not match React version', async () => {
90
+ expect(() => require('react-dom/server.edge')).toThrow(
91
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
92
+ 'must have the exact same version. Instead got:\n' +
93
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
94
+ ` - react-dom: ${actualReactVersion}`,
95
+ );
96
+ });
97
+
98
+ test('importing "react-dom/static" throws if version does not match React version', async () => {
99
+ expect(() => require('react-dom/static')).toThrow(
100
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
101
+ 'must have the exact same version. Instead got:\n' +
102
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
103
+ ` - react-dom: ${actualReactVersion}`,
104
+ );
105
+ });
106
+
107
+ test('importing "react-dom/static.node" throws if version does not match React version', async () => {
108
+ expect(() => require('react-dom/static.node')).toThrow(
109
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
110
+ 'must have the exact same version. Instead got:\n' +
111
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
112
+ ` - react-dom: ${actualReactVersion}`,
113
+ );
114
+ });
115
+
116
+ test('importing "react-dom/static.browser" throws if version does not match React version', async () => {
117
+ expect(() => require('react-dom/static.browser')).toThrow(
118
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
119
+ 'must have the exact same version. Instead got:\n' +
120
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
121
+ ` - react-dom: ${actualReactVersion}`,
122
+ );
123
+ });
124
+
125
+ test('importing "react-dom/static.edge" throws if version does not match React version', async () => {
126
+ expect(() => require('react-dom/static.edge')).toThrow(
127
+ 'Incompatible React versions: The "react" and "react-dom" packages ' +
128
+ 'must have the exact same version. Instead got:\n' +
129
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
130
+ ` - react-dom: ${actualReactVersion}`,
131
+ );
132
+ });
133
+
134
+ // @gate source
135
+ test('importing "react-native-renderer" throws if version does not match React version', async () => {
136
+ expect(() => require('react-native-renderer')).toThrow(
137
+ 'Incompatible React versions: The "react" and "react-native-renderer" packages ' +
138
+ 'must have the exact same version. Instead got:\n' +
139
+ ' - react: 18.0.0-whoa-this-aint-the-right-react\n' +
140
+ ` - react-native-renderer: ${actualReactVersion}`,
141
+ );
142
+ });
143
+});