Skip to content

Commit 2c17db2

Browse files
committed
fix(@angular/build): allow disabling Vitest isolation from builder
Keep the isolate builder option undefined when it is omitted so a Vitest runner config can provide its own value. Previously, ng test --isolate worked because true was forwarded as an override, but ng test --no-isolate was normalized to false and then treated the same as an omitted option. As a result, test.isolate: true in vitest-base.config.ts stayed active. Forward explicit false values to the Vitest project config so --no-isolate can override runnerConfig while the default still aligns with the Karma/Jasmine experience.
1 parent f472d2b commit 2c17db2

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

packages/angular/build/src/builders/unit-test/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function normalizeOptions(
126126
watch,
127127
debug: options.debug ?? false,
128128
ui: process.env['CI'] ? false : ui,
129-
isolate: isolate ?? false,
129+
isolate,
130130
quiet: options.quiet ?? (process.env['CI'] ? false : true),
131131
providersFile: options.providersFile && path.join(workspaceRoot, options.providersFile),
132132
setupFiles: options.setupFiles

packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ interface VitestConfigPluginOptions {
5454
include: string[];
5555
optimizeDepsInclude: string[];
5656
watch: boolean;
57-
isolate: boolean;
57+
isolate: boolean | undefined;
5858
}
5959

6060
async function findTestEnvironment(
@@ -268,8 +268,8 @@ export async function createVitestConfigPlugin(
268268
include,
269269
// CLI provider browser options override, if present
270270
...(browser ? { browser } : {}),
271-
// Only override if the user explicitly enabled it via CLI
272-
...(options.isolate ? { isolate: true } : {}),
271+
// Only override if the user explicitly set it via CLI
272+
...(options.isolate !== undefined ? { isolate: options.isolate } : {}),
273273
// If the user has not specified an environment, use a smart default.
274274
...(!testConfig?.environment
275275
? { environment: await findTestEnvironment(projectResolver) }

packages/angular/build/src/builders/unit-test/tests/options/isolate_spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,59 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
5454
const { result } = await harness.executeOnce();
5555
expect(result?.success).toBeTrue();
5656
});
57+
58+
it('should override isolate from the Vitest config file when set to false', async () => {
59+
harness.writeFile(
60+
'vitest-base.config.ts',
61+
`
62+
import { defineConfig } from 'vitest/config';
63+
64+
export default defineConfig({
65+
test: {
66+
fileParallelism: false,
67+
isolate: true,
68+
setupFiles: ['./src/setup.ts'],
69+
},
70+
});
71+
`,
72+
);
73+
74+
harness.writeFile(
75+
'src/setup.ts',
76+
`
77+
const global = globalThis as typeof globalThis & { setupCount?: number };
78+
global.setupCount = (global.setupCount ?? 0) + 1;
79+
`,
80+
);
81+
82+
harness.writeFile(
83+
'src/app/first.spec.ts',
84+
`
85+
it('runs first', () => {
86+
expect(true).toBe(true);
87+
});
88+
`,
89+
);
90+
91+
harness.writeFile(
92+
'src/app/second.spec.ts',
93+
`
94+
it('shares the test environment with the first spec file', () => {
95+
const global = globalThis as typeof globalThis & { setupCount?: number };
96+
expect(global.setupCount).toBeGreaterThan(1);
97+
});
98+
`,
99+
);
100+
101+
harness.useTarget('test', {
102+
...BASE_OPTIONS,
103+
runner: 'vitest' as any,
104+
runnerConfig: true,
105+
isolate: false,
106+
});
107+
108+
const { result } = await harness.executeOnce();
109+
expect(result?.success).toBeTrue();
110+
});
57111
});
58112
});

0 commit comments

Comments
 (0)