fix(core): add keepExistingVersions to jest option to preserve dependency versions (#30652)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Currently, when we use the jest configuration generator it will forcibly
update the jest version if the package already exist.


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Now, the jest version will be preserved unless the option is passed to
update the version.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Nicholas Cunningham 2025-05-07 10:01:10 -06:00 committed by GitHub
parent 480a20e3c5
commit 3b3b320ff7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 43 additions and 1 deletions

View File

@ -79,6 +79,12 @@
"runtimeTsconfigFileName": { "runtimeTsconfigFileName": {
"type": "string", "type": "string",
"description": "The name of the project's tsconfig file that includes the runtime source files. If not provided, it will default to `tsconfig.lib.json` for libraries and `tsconfig.app.json` for applications." "description": "The name of the project's tsconfig file that includes the runtime source files. If not provided, it will default to `tsconfig.lib.json` for libraries and `tsconfig.app.json` for applications."
},
"keepExistingVersions": {
"type": "boolean",
"x-priority": "internal",
"description": "Keep existing dependencies versions",
"default": true
} }
}, },
"required": [], "required": [],

View File

@ -2,12 +2,15 @@ import {
checkFilesDoNotExist, checkFilesDoNotExist,
checkFilesExist, checkFilesExist,
cleanupProject, cleanupProject,
detectPackageManager,
getPackageManagerCommand,
newProject, newProject,
readFile, readFile,
readJson, readJson,
rmDist, rmDist,
runCLI, runCLI,
runCLIAsync, runCLIAsync,
runCommand,
uniq, uniq,
updateFile, updateFile,
updateJson, updateJson,
@ -187,4 +190,23 @@ describe('js e2e', () => {
'Test Suites: 1 passed, 1 total' 'Test Suites: 1 passed, 1 total'
); );
}, 500_000); }, 500_000);
it('should not update dependencies if they already exist', () => {
const lib = uniq('@my-org/mylib');
const currentJestVersion = '28.0.0';
const pm = detectPackageManager();
// set jest version
updateJson('package.json', (json) => {
json.devDependencies['jest'] = currentJestVersion;
return json;
});
runCommand(getPackageManagerCommand({ packageManager: pm }).install);
runCLI(`generate @nx/js:lib ${lib} --bundler=tsc --unitTestRunner=jest`);
const jestVersionAfterInstall =
readJson('package.json').devDependencies['jest'];
expect(jestVersionAfterInstall).toEqual(currentJestVersion);
});
}); });

View File

@ -71,6 +71,7 @@ function normalizeOptions(
return { return {
...schemaDefaults, ...schemaDefaults,
...options, ...options,
keepExistingVersions: options.keepExistingVersions ?? true,
rootProject: project.root === '.' || project.root === '', rootProject: project.root === '.' || project.root === '',
isTsSolutionSetup: isUsingTsSolutionSetup(tree), isTsSolutionSetup: isUsingTsSolutionSetup(tree),
}; };

View File

@ -44,5 +44,11 @@ export function ensureDependencies(
devDeps['@swc/jest'] = swcJestVersion; devDeps['@swc/jest'] = swcJestVersion;
} }
return addDependenciesToPackageJson(tree, dependencies, devDeps); return addDependenciesToPackageJson(
tree,
dependencies,
devDeps,
undefined,
options.keepExistingVersions
);
} }

View File

@ -30,6 +30,7 @@ export interface JestProjectSchema {
* @deprecated Use the `setupFile` option instead. It will be removed in Nx v22. * @deprecated Use the `setupFile` option instead. It will be removed in Nx v22.
*/ */
skipSetupFile?: boolean; skipSetupFile?: boolean;
keepExistingVersions?: boolean;
} }
export type NormalizedJestProjectSchema = JestProjectSchema & { export type NormalizedJestProjectSchema = JestProjectSchema & {

View File

@ -78,6 +78,12 @@
"runtimeTsconfigFileName": { "runtimeTsconfigFileName": {
"type": "string", "type": "string",
"description": "The name of the project's tsconfig file that includes the runtime source files. If not provided, it will default to `tsconfig.lib.json` for libraries and `tsconfig.app.json` for applications." "description": "The name of the project's tsconfig file that includes the runtime source files. If not provided, it will default to `tsconfig.lib.json` for libraries and `tsconfig.app.json` for applications."
},
"keepExistingVersions": {
"type": "boolean",
"x-priority": "internal",
"description": "Keep existing dependencies versions",
"default": true
} }
}, },
"required": [] "required": []