diff --git a/tests/__helpers__/nodeVersion.ts b/tests/__helpers__/nodeVersion.ts new file mode 100644 index 000000000..9ec4b4d08 --- /dev/null +++ b/tests/__helpers__/nodeVersion.ts @@ -0,0 +1,9 @@ +import * as Semver from 'semver' + +export const getNodeMajorVersion = () => { + const parsedNodeVersion = Semver.parse(process.version) + if (!parsedNodeVersion) { + throw new Error(`Failed to parse node version: ${process.version}`) + } + return parsedNodeVersion.major +} diff --git a/tests/__providers__/project.ts b/tests/__providers__/project.ts index 5806b20e0..34279c32c 100644 --- a/tests/__providers__/project.ts +++ b/tests/__providers__/project.ts @@ -4,6 +4,7 @@ import { merge } from 'lodash' import readPkgUp from 'read-pkg-up' import { PackageJson, TsConfigJson } from 'type-fest' import { assertBuildPresent } from '../__helpers__/helpers' +import { getNodeMajorVersion } from '../__helpers__/nodeVersion' import { getDynamicPackagesVersions } from '../__helpers__/packagesVersions' import type * as Run from './run' @@ -14,6 +15,7 @@ type Project = { } packageJson: { mergeAsync(fields: PackageJson): Promise + mergeIfExistAsync(fields: PackageJson): Promise createAsync(fields: PackageJson): Promise } tsconfig: { @@ -52,9 +54,10 @@ export const project = () => await ctx.fs.copyAsync(path, ctx.fs.cwd(), { overwrite: true, }) - await api.packageJson.mergeAsync({ + await api.packageJson.mergeIfExistAsync({ devDependencies: { '@prisma/client': dynamicPackagesVersions['@prisma/client'], + '@types/node': `^${getNodeMajorVersion()}.0.0`, prisma: dynamicPackagesVersions.prisma, graphql: dynamicPackagesVersions.graphql, nexus: dynamicPackagesVersions.nexus, @@ -73,6 +76,21 @@ export const project = () => const PackageJsonNew = merge(PackageJson, fields) await ctx.fs.writeAsync(fileName, PackageJsonNew, { jsonIndent: 2 }) }, + mergeIfExistAsync: async (fields) => { + const fileName = 'package.json' + const PackageJson = await ctx.fs.readAsync(fileName, 'json') + const PackageJsonNew = Object.assign({}, PackageJson) + Object.keys(fields).forEach((key) => { + const destinationField = (PackageJsonNew[key] ??= {}) + const sourceField = fields[key] as Record + Object.keys(sourceField).forEach((subKey) => { + if (destinationField[subKey] != null) { + destinationField[subKey] = sourceField[subKey] + } + }) + }) + await ctx.fs.writeAsync(fileName, PackageJsonNew, { jsonIndent: 2 }) + }, }, tsconfig: { createAsync: async (tsconfig) => { diff --git a/tests/e2e/fixtures/esm/package.json b/tests/e2e/fixtures/esm/package.json index cb7de69f6..b28980d54 100644 --- a/tests/e2e/fixtures/esm/package.json +++ b/tests/e2e/fixtures/esm/package.json @@ -9,6 +9,7 @@ }, "devDependencies": { "@prisma/client": "__dynamic__", + "@types/node": "__dynamic__", "graphql": "__dynamic__", "nexus": "__dynamic__", "prisma": "__dynamic__",