diff --git a/src/schematics/ng-add/index.spec.ts b/src/schematics/ng-add/index.spec.ts index 5bd0865..74254da 100644 --- a/src/schematics/ng-add/index.spec.ts +++ b/src/schematics/ng-add/index.spec.ts @@ -53,5 +53,9 @@ describe('ng-add', () => { const packageJSON = JSON.parse(tree.readContent('/package.json')); expect(packageJSON.devDependencies['@playwright/test']).toBeTruthy(); + // check that the dependency is added in the correct place + expect(Object.keys(packageJSON.devDependencies)).toEqual( + Object.keys(packageJSON.devDependencies).sort(), + ); }); }); diff --git a/src/schematics/ng-add/index.ts b/src/schematics/ng-add/index.ts index a7a3cf2..b0361d2 100644 --- a/src/schematics/ng-add/index.ts +++ b/src/schematics/ng-add/index.ts @@ -106,6 +106,7 @@ function addPackageToPackageJson( if (!json.devDependencies[pkg]) { json.devDependencies[pkg] = version; } + json.devDependencies = sortObjectByKeys(json.devDependencies); tree.overwrite('package.json', JSON.stringify(json, null, 2)); return tree; @@ -123,3 +124,17 @@ function addPlaywright(tree: Tree, context: SchematicContext) { PLAYWRIGHT_TEST_VERSION, ); } + +function sortObjectByKeys( + obj: Record, +): Record { + return Object.keys(obj) + .sort() + .reduce((result, key) => { + return { + // biome-ignore lint/performance/noAccumulatingSpread: small object, no perf cost + ...result, + [key]: obj[key], + }; + }, {}); +}