Skip to content

Commit fcdbf6c

Browse files
yjaaidihybrist
authored andcommitted
fix(@angular/build): allow globals to be set to false
Closes #31785 (cherry picked from commit 5e78117)
1 parent 55145f5 commit fcdbf6c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function createTestBedInitVirtualFile(
3333
import { NgModule${usesZoneJS ? ', provideZoneChangeDetection' : ''} } from '@angular/core';
3434
import { getTestBed, ɵgetCleanupHook as getCleanupHook } from '@angular/core/testing';
3535
import { BrowserTestingModule, platformBrowserTesting } from '@angular/platform-browser/testing';
36+
import { afterEach, beforeEach } from 'vitest';
3637
${providersImport}
3738
3839
const ANGULAR_TESTBED_SETUP = Symbol.for('@angular/cli/testbed-setup');

packages/angular/build/src/builders/unit-test/tests/behavior/runner-config-vitest_spec.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
8989
expect(results.numPassedTests).toBe(1);
9090
});
9191

92-
it('should allow overriding builder options via runnerConfig file', async () => {
92+
it('should allow overriding globals to false via runnerConfig file', async () => {
9393
harness.useTarget('test', {
9494
...BASE_OPTIONS,
9595
runnerConfig: 'vitest.config.ts',
@@ -111,7 +111,7 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
111111
harness.writeFile(
112112
'src/app/app.component.spec.ts',
113113
`
114-
import { vi, test, expect } from 'vitest';
114+
import { expect } from 'vitest';
115115
test('should pass', () => {
116116
expect(true).toBe(true);
117117
});
@@ -122,6 +122,38 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
122122
expect(result?.success).toBeFalse();
123123
});
124124

125+
it('should initialize environment even when globals are disabled in runnerConfig file', async () => {
126+
harness.useTarget('test', {
127+
...BASE_OPTIONS,
128+
runnerConfig: 'vitest.config.ts',
129+
});
130+
131+
harness.writeFile(
132+
'vitest.config.ts',
133+
`
134+
import { defineConfig } from 'vitest/config';
135+
export default defineConfig({
136+
test: {
137+
globals: false,
138+
},
139+
});
140+
`,
141+
);
142+
143+
harness.writeFile(
144+
'src/app/app.component.spec.ts',
145+
`
146+
import { test, expect } from 'vitest';
147+
test('should pass', () => {
148+
expect(true).toBe(true);
149+
});
150+
`,
151+
);
152+
153+
const { result } = await harness.executeOnce();
154+
expect(result?.success).toBeTrue();
155+
});
156+
125157
it('should fail when a DOM-dependent test is run in a node environment', async () => {
126158
harness.useTarget('test', {
127159
...BASE_OPTIONS,

0 commit comments

Comments
 (0)