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