Skip to content

Commit 9fa8930

Browse files
cogwirrelColy010
andauthored
fix(vite): ensure test target dependency is not duplicated (#30289)
<!-- 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 Every time the vitest generator was run a new duplicate '^build' dependency was added to the target defaults for the test task. ## Expected Behavior No duplicate entries added! ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #30288 --------- Co-authored-by: Colum Ferry <[email protected]>
1 parent bae3acd commit 9fa8930

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

packages/vite/src/generators/vitest/vitest-generator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ getTestBed().initTestEnvironment(
171171
nxJson.targetDefaults ??= {};
172172
nxJson.targetDefaults[testTarget] ??= {};
173173
nxJson.targetDefaults[testTarget].dependsOn ??= [];
174-
nxJson.targetDefaults[testTarget].dependsOn.push('^build');
174+
nxJson.targetDefaults[testTarget].dependsOn = Array.from(
175+
new Set([...nxJson.targetDefaults[testTarget].dependsOn, '^build'])
176+
);
175177
updateNxJson(tree, nxJson);
176178
}
177179

packages/vite/src/generators/vitest/vitest.spec.ts

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ describe('vitest generator', () => {
246246
});
247247

248248
describe('TS solution setup', () => {
249+
const addProject = (name: string) => {
250+
addProjectConfiguration(appTree, name, {
251+
root: `packages/${name}`,
252+
sourceRoot: `packages/${name}/src`,
253+
targets: {
254+
lint: {
255+
executor: '@nx/eslint:lint',
256+
options: {},
257+
},
258+
},
259+
});
260+
writeJson(appTree, `packages/${name}/tsconfig.json`, {
261+
files: [],
262+
include: [],
263+
references: [],
264+
});
265+
};
266+
249267
beforeEach(() => {
250268
appTree = createTreeWithEmptyWorkspace();
251269
updateJson(appTree, 'package.json', (json) => {
@@ -261,21 +279,7 @@ describe('vitest generator', () => {
261279
references: [],
262280
});
263281

264-
addProjectConfiguration(appTree, 'pkg1', {
265-
root: 'packages/pkg1',
266-
sourceRoot: 'packages/pkg1/src',
267-
targets: {
268-
lint: {
269-
executor: '@nx/eslint:lint',
270-
options: {},
271-
},
272-
},
273-
});
274-
writeJson(appTree, 'packages/pkg1/tsconfig.json', {
275-
files: [],
276-
include: [],
277-
references: [],
278-
});
282+
addProject('pkg1');
279283
});
280284

281285
it('should add a tsconfig.spec.json file', async () => {
@@ -331,6 +335,21 @@ describe('vitest generator', () => {
331335
const nxJson = readNxJson(appTree);
332336
expect(nxJson.targetDefaults.test.dependsOn).toStrictEqual(['^build']);
333337
});
338+
339+
it(`should not duplicate the test target dependency on the deps' build target`, async () => {
340+
await generator(appTree, {
341+
project: 'pkg1',
342+
coverageProvider: 'v8',
343+
});
344+
addProject('pkg2');
345+
await generator(appTree, {
346+
project: 'pkg2',
347+
coverageProvider: 'v8',
348+
});
349+
350+
const nxJson = readNxJson(appTree);
351+
expect(nxJson.targetDefaults.test.dependsOn).toStrictEqual(['^build']);
352+
});
334353
});
335354
});
336355

0 commit comments

Comments
 (0)