Skip to content

test: pick highest version of react from peerDependencies #2637

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

Merged
merged 3 commits into from
Oct 1, 2024
Merged
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
30 changes: 28 additions & 2 deletions tests/utils/next-version-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { readFile, writeFile } from 'node:fs/promises'

import fg from 'fast-glob'
import { gte, satisfies, valid } from 'semver'
import { coerce, gt, gte, satisfies, valid } from 'semver'
import { execaCommand } from 'execa'

const FUTURE_NEXT_PATCH_VERSION = '14.999.0'
Expand Down Expand Up @@ -105,10 +105,36 @@ export async function setNextVersionInFixture(
const nextPeerDependencies = JSON.parse(stdout)

if (updateReact && nextVersionRequiresReact19(checkVersion)) {
// canaries started reporting peerDependencies as `^18.2.0 || 19.0.0-rc-<hash>-<date>`
// with https://github.com/vercel/next.js/pull/70219 which is valid range for package managers
// but not for @nx/next which checks dependencies and tries to assure that at least React 18 is used
// but the check doesn't handle the alternative in version selector which thinks it's not valid:
// https://github.com/nrwl/nx/blob/8fa7065cf14df6a90896442f90659b00baa1b5b9/packages/next/src/executors/build/build.impl.ts#L48
// https://github.com/nrwl/nx/blob/8fa7065cf14df6a90896442f90659b00baa1b5b9/packages/devkit/src/utils/semver.ts#L17
// so to workaround this nx/next issue we modify next peerDeps to extract highest version alternative to use
const nextReactPeerDependency = nextPeerDependencies['react'] ?? '^18.2.0'
const highestNextReactPeerDependencySelector = nextReactPeerDependency
.split('||')
.map((alternative) => {
const selector = alternative.trim()
const coerced = coerce(selector)?.format()
return {
selector,
coerced,
}
})
.sort((a, b) => {
return gt(a.coerced, b.coerced) ? -1 : 1
})[0].selector

const reactVersion =
operation === 'update' ? nextPeerDependencies['react'] : REACT_18_VERSION
operation === 'update' ? highestNextReactPeerDependencySelector : REACT_18_VERSION
packageJson.dependencies.react = reactVersion
packageJson.dependencies['react-dom'] = reactVersion

if (!silent) {
console.log(`${logPrefix}▲ Setting react(-dom)@${reactVersion}`)
}
}

await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n')
Expand Down
Loading