Skip to content

Commit 8e26051

Browse files
committed
test: pick highest version of react from peerDependencies
1 parent a3240f3 commit 8e26051

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

tests/utils/next-version-helpers.mjs

+27-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { readFile, writeFile } from 'node:fs/promises'
44

55
import fg from 'fast-glob'
6-
import { gte, satisfies, valid } from 'semver'
6+
import { coerce, gt, gte, satisfies, valid } from 'semver'
77
import { execaCommand } from 'execa'
88

99
const FUTURE_NEXT_PATCH_VERSION = '14.999.0'
@@ -105,8 +105,33 @@ export async function setNextVersionInFixture(
105105
const nextPeerDependencies = JSON.parse(stdout)
106106

107107
if (updateReact && nextVersionRequiresReact19(checkVersion)) {
108+
// canaries started reporting peerDependencies as `^18.2.0 || 19.0.0-rc-<hash>-<date>`
109+
// with https://github.com/vercel/next.js/pull/70219 which is valid range for package managers
110+
// but not for @nx/next which checks dependencies and tries to assure that at least React 18 is used
111+
// but the check doesn't handle the alternative in version selector which thinks it's not valid:
112+
// https://github.com/nrwl/nx/blob/8fa7065cf14df6a90896442f90659b00baa1b5b9/packages/next/src/executors/build/build.impl.ts#L48
113+
// https://github.com/nrwl/nx/blob/8fa7065cf14df6a90896442f90659b00baa1b5b9/packages/devkit/src/utils/semver.ts#L17
114+
// so to workaround this nx/next issue we modify next peerDeps to extract highest version alternative to use
115+
const highestNextReactPeerDependencySelector = (
116+
nextPeerDependencies['react'] ?? '^18.2.0'
117+
)
118+
.split('||')
119+
.map((alternative) => {
120+
const selector = alternative.trim()
121+
const coerced = coerce(selector)?.format()
122+
return {
123+
selector,
124+
coerced,
125+
validSelector: valid(selector),
126+
validCoerced: valid(coerced),
127+
}
128+
})
129+
.sort((a, b) => {
130+
return gt(a.coerced, b.coerced) ? -1 : 1
131+
})[0].selector
132+
108133
const reactVersion =
109-
operation === 'update' ? nextPeerDependencies['react'] : REACT_18_VERSION
134+
operation === 'update' ? highestNextReactPeerDependencySelector : REACT_18_VERSION
110135
packageJson.dependencies.react = reactVersion
111136
packageJson.dependencies['react-dom'] = reactVersion
112137
}

0 commit comments

Comments
 (0)