Unverified Commit a6e478f9 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Use require alias to resolve react 18 for testing (#27601)



For test only, previously the react-dom is still been resolved as react 17

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
parent e6d12a9d
...@@ -8,6 +8,7 @@ module.exports = { ...@@ -8,6 +8,7 @@ module.exports = {
// Use react 18 // Use react 18
alias['react'] = require.resolve('react-18') alias['react'] = require.resolve('react-18')
alias['react-dom'] = require.resolve('react-dom-18') alias['react-dom'] = require.resolve('react-dom-18')
alias['react-dom/server'] = require.resolve('react-dom-18/server')
return config return config
}, },
......
{ {
"name": "react-dom", "name": "react-dom",
"version": "18.0.0-alpha-e6be2d531" "version": "18.0.0-alpha-c76e4dbbc-20210722"
} }
export default function Bar() {
return <div>bar</div>
}
import { Suspense } from 'react'
import Bar from './bar'
export default function Index() { export default function Index() {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
window.didHydrate = true window.didHydrate = true
} }
return <p>Hello</p> return (
<div>
<p>Hello</p>
<Suspense fallback={'loading...'}>
<Bar />
</Suspense>
</div>
)
} }
...@@ -7,13 +7,14 @@ import { ...@@ -7,13 +7,14 @@ import {
findPort, findPort,
killApp, killApp,
launchApp, launchApp,
runNextCommand,
nextBuild, nextBuild,
nextStart, nextStart,
} from 'next-test-utils' } from 'next-test-utils'
jest.setTimeout(1000 * 60 * 5) jest.setTimeout(1000 * 60 * 5)
// overrides react and react-dom to v18
const nodeArgs = ['-r', join(__dirname, 'require-hook.js')]
const dirSupported = join(__dirname, '../supported') const dirSupported = join(__dirname, '../supported')
const dirPrerelease = join(__dirname, '../prerelease') const dirPrerelease = join(__dirname, '../prerelease')
...@@ -22,9 +23,10 @@ const UNSUPPORTED_PRERELEASE = ...@@ -22,9 +23,10 @@ const UNSUPPORTED_PRERELEASE =
const USING_CREATE_ROOT = 'Using the createRoot API for React' const USING_CREATE_ROOT = 'Using the createRoot API for React'
async function getBuildOutput(dir) { async function getBuildOutput(dir) {
const { stdout, stderr } = await runNextCommand(['build', dir], { const { stdout, stderr } = await nextBuild(dir, [], {
stdout: true, stdout: true,
stderr: true, stderr: true,
nodeArgs,
}) })
return stdout + stderr return stdout + stderr
} }
...@@ -43,6 +45,7 @@ async function getDevOutput(dir) { ...@@ -43,6 +45,7 @@ async function getDevOutput(dir) {
onStderr(msg) { onStderr(msg) {
stderr += msg stderr += msg
}, },
nodeArgs,
}) })
await killApp(instance) await killApp(instance)
return stdout + stderr return stdout + stderr
...@@ -83,11 +86,17 @@ describe('React 18 Support', () => { ...@@ -83,11 +86,17 @@ describe('React 18 Support', () => {
let appPort let appPort
beforeAll(async () => { beforeAll(async () => {
await fs.remove(join(appDir, '.next')) await fs.remove(join(appDir, '.next'))
await nextBuild(appDir, [dirPrerelease]) await nextBuild(appDir, [dirPrerelease], {
nodeArgs,
stdout: true,
stderr: true,
})
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort, { nodeArgs })
})
afterAll(async () => {
await killApp(app)
}) })
afterAll(async () => await killApp(app))
it('hydrates correctly for normal page', async () => { it('hydrates correctly for normal page', async () => {
const browser = await webdriver(appPort, '/') const browser = await webdriver(appPort, '/')
expect(await browser.eval('window.didHydrate')).toBe(true) expect(await browser.eval('window.didHydrate')).toBe(true)
......
const mod = require('module')
const hookPropertyMap = new Map([
['react', 'react-18'],
['react-dom', 'react-dom-18'],
['react-dom/server', 'react-dom-18/server'],
])
const resolveFilename = mod._resolveFilename
mod._resolveFilename = function (request, parent, isMain, options) {
const hookResolved = hookPropertyMap.get(request)
if (hookResolved) request = hookResolved
return resolveFilename.call(mod, request, parent, isMain, options)
}
...@@ -106,12 +106,16 @@ export function runNextCommand(argv, options = {}) { ...@@ -106,12 +106,16 @@ export function runNextCommand(argv, options = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log(`Running command "next ${argv.join(' ')}"`) console.log(`Running command "next ${argv.join(' ')}"`)
const instance = spawn('node', ['--no-deprecation', nextBin, ...argv], { const instance = spawn(
'node',
[...(options.nodeArgs || []), '--no-deprecation', nextBin, ...argv],
{
...options.spawnOptions, ...options.spawnOptions,
cwd, cwd,
env, env,
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
}) }
)
if (typeof options.instance === 'function') { if (typeof options.instance === 'function') {
options.instance(instance) options.instance(instance)
...@@ -176,11 +180,16 @@ export function runNextCommandDev(argv, stdOut, opts = {}) { ...@@ -176,11 +180,16 @@ export function runNextCommandDev(argv, stdOut, opts = {}) {
...opts.env, ...opts.env,
} }
const nodeArgs = opts.nodeArgs || []
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const instance = spawn('node', ['--no-deprecation', nextBin, ...argv], { const instance = spawn(
'node',
[...nodeArgs, '--no-deprecation', nextBin, ...argv],
{
cwd, cwd,
env, env,
}) }
)
let didResolve = false let didResolve = false
function handleStdout(data) { function handleStdout(data) {
......
...@@ -16045,21 +16045,21 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.2.7, rc@^1.2.8: ...@@ -16045,21 +16045,21 @@ rc@^1.0.1, rc@^1.1.6, rc@^1.2.7, rc@^1.2.8:
strip-json-comments "~2.0.1" strip-json-comments "~2.0.1"
"react-18@npm:react@next": "react-18@npm:react@next":
version "18.0.0-alpha-73ffce1b6-20210624" version "18.0.0-alpha-c76e4dbbc-20210722"
resolved "https://registry.yarnpkg.com/react/-/react-18.0.0-alpha-73ffce1b6-20210624.tgz#d9fb8700c6fad8de752ec0427f2ae3a941eea951" resolved "https://registry.yarnpkg.com/react/-/react-18.0.0-alpha-c76e4dbbc-20210722.tgz#a1591fa4926bb1c514552f77efcc4b4744448f2f"
integrity sha512-Qaj2vhrMlYc169Yh0gXBB7WeKWMeIVx99JnuouuT71Jku2Cly9TxAWurc+h6PSgz/qjjmDA2NOtHCb6mGlmzGA== integrity sha512-dtRjRJVHPA25oi29ZFEuLX6R7Rdk5ikCamiDRD0LCB1IpR5hiGKGcNjSxU/rN7rAxs8iRggi15Fg9k1jAK7oBQ==
dependencies: dependencies:
loose-envify "^1.1.0" loose-envify "^1.1.0"
object-assign "^4.1.1" object-assign "^4.1.1"
"react-dom-18@npm:react-dom@next": "react-dom-18@npm:react-dom@next":
version "18.0.0-alpha-73ffce1b6-20210624" version "18.0.0-alpha-c76e4dbbc-20210722"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.0.0-alpha-73ffce1b6-20210624.tgz#3d789e4f3446abc685a7754c8dc74dea0ffb4247" resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.0.0-alpha-c76e4dbbc-20210722.tgz#4fc4518926332983788d48aaad0c6ca5e5d806e5"
integrity sha512-TgA+VhVas3mJdhy6AQLXnPzBN2JeNKC7EGhLKU11XOxUODCGQ94nyT04i1ta2R3Fv0QevMLp0Wb5hccan0wMEg== integrity sha512-lV/+kWzfPq8HLdFnDN1UrJ5blXVFRYCrdOJyZyr9YMZODBl63kpPR7gtszSC4lWiPiMw6K8X+4a3UFU96L7pHQ==
dependencies: dependencies:
loose-envify "^1.1.0" loose-envify "^1.1.0"
object-assign "^4.1.1" object-assign "^4.1.1"
scheduler "0.21.0-alpha-73ffce1b6-20210624" scheduler "0.21.0-alpha-c76e4dbbc-20210722"
react-dom@17.0.2: react-dom@17.0.2:
version "17.0.2" version "17.0.2"
...@@ -17093,10 +17093,10 @@ saxes@^5.0.1: ...@@ -17093,10 +17093,10 @@ saxes@^5.0.1:
dependencies: dependencies:
xmlchars "^2.2.0" xmlchars "^2.2.0"
scheduler@0.21.0-alpha-73ffce1b6-20210624: scheduler@0.21.0-alpha-c76e4dbbc-20210722:
version "0.21.0-alpha-73ffce1b6-20210624" version "0.21.0-alpha-c76e4dbbc-20210722"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.21.0-alpha-73ffce1b6-20210624.tgz#6fff95e26af73cfaa365b68fa3b68c4c66dfe347" resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.21.0-alpha-c76e4dbbc-20210722.tgz#7172827a6ed0829e41d1c07cbbf7d9fea6b018ba"
integrity sha512-7SXTiepGRo63F5Yp/fxLhZDYi5TInsqjnMTYF6GwtunUGAwyuK4V/AFiF0Q1gtB32U/e+C+OE4SSj9LtBYBjYw== integrity sha512-X4Xv2zq1OXxsQNR48reXnOQ9kGnS/mY0cCAusJ6Sm/JBUT83+zetG2sPZPFQSS1np2zkmgMa4+f5H/t2U6AW6Q==
dependencies: dependencies:
loose-envify "^1.1.0" loose-envify "^1.1.0"
object-assign "^4.1.1" object-assign "^4.1.1"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment