Skip to content

Commit 462ecb0

Browse files
committed
test: ✅ upgrade vitest to v4
1 parent 3e29f76 commit 462ecb0

File tree

265 files changed

+3354
-3024
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

265 files changed

+3354
-3024
lines changed

apps/101-meal-planner-solution/src/test-setup-common.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import './styles.css';
12
import '@testing-library/jest-dom';
2-
import './test-setup-common';
3+
import { setUpTestBed } from './testing/set-up-test-bed';
34

45
const originalItTodo = it.todo.bind(it);
56
/* Strip extra arguments to align with vitest and avoid the following error:
67
* "todo must be called with only a description." */
78
it.todo = (name: string) => originalItTodo(name);
9+
10+
setUpTestBed();
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1+
import './styles.css';
12
import '@testing-library/jest-dom/vitest';
2-
import './test-setup-common';
3+
import { setUpTestBed } from './testing/set-up-test-bed';
34

4-
/**
5-
* When using `forks` or `threads` pools, Vitest does not override the global `window`,
6-
* Therefore, when Angular CDK harness creates an event using `window` as the view,
7-
* the `window` reference is different from jsdom's, and this causes the following error:
8-
* TypeError: Failed to construct 'KeyboardEvent': member view is not of type Window.
9-
*
10-
* The workaround is to override the `window` global variable.
11-
* This only fixes code that uses `window` but not code using `globalThis`.
12-
*
13-
* @see https://github.com/angular/components/blob/3c84525dd23cfb20691ae07e4e39caa36c354af4/src/cdk/testing/testbed/fake-events/event-objects.ts#L142
14-
* @see https://github.com/vitest-dev/vitest/issues/4685
15-
*/
16-
beforeEach(() => {
17-
if (g.jsdom && g.jsdom.window !== g.window) {
18-
vi.spyOn(g, 'window', 'get').mockImplementation(() => g.jsdom.window);
19-
}
20-
});
21-
22-
const g = globalThis as unknown as typeof globalThis & {
23-
jsdom: { window: Window & typeof globalThis };
24-
};
5+
setUpTestBed();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { NgModule, provideZonelessChangeDetection } from '@angular/core';
2+
import { getTestBed } from '@angular/core/testing';
3+
import {
4+
BrowserTestingModule,
5+
platformBrowserTesting,
6+
} from '@angular/platform-browser/testing';
7+
8+
declare global {
9+
const DEBUG_BROWSER: boolean | undefined;
10+
}
11+
12+
export function setUpTestBed() {
13+
/* When debugging in the browser, we want to prevent Angular Testing Library
14+
* and TestBed from cleaning up after the test in order to interact with the browser.
15+
* Therefore, we disable Angular Testing Library clean up and forward the DEBUG_BROWSER
16+
* environment variable to move TestBed cleanup from afterEach to beforeEach. */
17+
const isDebugBrowserMode =
18+
typeof DEBUG_BROWSER !== 'undefined' && DEBUG_BROWSER;
19+
const lifecycleHook = isDebugBrowserMode ? beforeEach : afterEach;
20+
lifecycleHook(() => getTestBed().resetTestingModule());
21+
22+
const testBed = getTestBed();
23+
if (testBed.platform) {
24+
return;
25+
}
26+
27+
@NgModule({ providers: [provideZonelessChangeDetection()] })
28+
class TestModule {}
29+
30+
testBed.initTestEnvironment(
31+
[BrowserTestingModule, TestModule],
32+
platformBrowserTesting(),
33+
{
34+
teardown: { destroyAfterEach: false },
35+
},
36+
);
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineConfig, mergeConfig } from 'vitest/config';
2+
3+
export default mergeConfig(
4+
defineConfig({
5+
define: {
6+
/* When debugging in the browser, we want to prevent TestBed from cleaning up after
7+
* the test and we don't want Angular Testing Library to interfere. */
8+
'process.env.ATL_SKIP_AUTO_CLEANUP': 'true',
9+
},
10+
}),
11+
defineConfig(
12+
process.env.DEBUG_BROWSER != null
13+
? {
14+
/* When debugging in the browser, we want to prevent TestBed from cleaning up after
15+
* the test in order to interact with the browser.
16+
* Therefore, we forward the DEBUG_BROWSER environment variable to move TestBed cleanup
17+
* from afterEach to beforeEach. */
18+
define: { DEBUG_BROWSER: true },
19+
test: { fileParallelism: false },
20+
}
21+
: {},
22+
),
23+
);

apps/101-meal-planner-solution/vitest.config.mts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1+
import { playwright } from '@vitest/browser-playwright';
12
import { defineConfig, mergeConfig } from 'vitest/config';
23
import viteConfig from './vite.config.mjs';
4+
import vitestAngularPreset from './vitest-angular-preset';
35

46
const testPatterns = ['src/**/*.spec.ts'];
57
const browserTestPatterns = ['src/**/*.browser.spec.ts'];
68

79
export default mergeConfig(
8-
viteConfig,
10+
mergeConfig(viteConfig, vitestAngularPreset),
911
defineConfig({
10-
/* When debugging in the browser, we want to prevent Angular Testing Library
11-
* and TestBed from cleaning up after the test in order to interact with the browser.
12-
* Therefore, we disable Angular Testing Library clean up and forward the DEBUG_BROWSER
13-
* environment variable to move TestBed cleanup from afterEach to beforeEach. */
14-
define:
15-
process.env.DEBUG_BROWSER != null
16-
? {
17-
'process.env.ATL_SKIP_AUTO_CLEANUP': 'true',
18-
'process.env.DEBUG_BROWSER': 'true',
19-
}
20-
: {},
2112
test: {
2213
globals: true,
2314
setupFiles: ['src/test-setup.ts'],
@@ -28,6 +19,7 @@ export default mergeConfig(
2819
},
2920
watch: false,
3021
pool: 'threads',
22+
isolate: false,
3123
projects: [
3224
{
3325
extends: true,
@@ -45,7 +37,7 @@ export default mergeConfig(
4537
include: browserTestPatterns,
4638
browser: {
4739
enabled: true,
48-
provider: 'playwright',
40+
provider: playwright(),
4941
instances: [{ browser: 'chromium' }],
5042
},
5143
},

apps/101-meal-planner-starter/src/test-setup-common.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import './styles.css';
12
import '@testing-library/jest-dom';
2-
import './test-setup-common';
3+
import { setUpTestBed } from './testing/set-up-test-bed';
34

45
const originalItTodo = it.todo.bind(it);
56
/* Strip extra arguments to align with vitest and avoid the following error:
67
* "todo must be called with only a description." */
78
it.todo = (name: string) => originalItTodo(name);
9+
10+
setUpTestBed();
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1+
import './styles.css';
12
import '@testing-library/jest-dom/vitest';
2-
import './test-setup-common';
3+
import { setUpTestBed } from './testing/set-up-test-bed';
34

4-
/**
5-
* When using `forks` or `threads` pools, Vitest does not override the global `window`,
6-
* Therefore, when Angular CDK harness creates an event using `window` as the view,
7-
* the `window` reference is different from jsdom's, and this causes the following error:
8-
* TypeError: Failed to construct 'KeyboardEvent': member view is not of type Window.
9-
*
10-
* The workaround is to override the `window` global variable.
11-
* This only fixes code that uses `window` but not code using `globalThis`.
12-
*
13-
* @see https://github.com/angular/components/blob/3c84525dd23cfb20691ae07e4e39caa36c354af4/src/cdk/testing/testbed/fake-events/event-objects.ts#L142
14-
* @see https://github.com/vitest-dev/vitest/issues/4685
15-
*/
16-
beforeEach(() => {
17-
if (g.jsdom && g.jsdom.window !== g.window) {
18-
vi.spyOn(g, 'window', 'get').mockImplementation(() => g.jsdom.window);
19-
}
20-
});
21-
22-
const g = globalThis as unknown as typeof globalThis & {
23-
jsdom: { window: Window & typeof globalThis };
24-
};
5+
setUpTestBed();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { NgModule, provideZonelessChangeDetection } from '@angular/core';
2+
import { getTestBed } from '@angular/core/testing';
3+
import {
4+
BrowserTestingModule,
5+
platformBrowserTesting,
6+
} from '@angular/platform-browser/testing';
7+
8+
declare global {
9+
const DEBUG_BROWSER: boolean | undefined;
10+
}
11+
12+
export function setUpTestBed() {
13+
/* When debugging in the browser, we want to prevent Angular Testing Library
14+
* and TestBed from cleaning up after the test in order to interact with the browser.
15+
* Therefore, we disable Angular Testing Library clean up and forward the DEBUG_BROWSER
16+
* environment variable to move TestBed cleanup from afterEach to beforeEach. */
17+
const isDebugBrowserMode =
18+
typeof DEBUG_BROWSER !== 'undefined' && DEBUG_BROWSER;
19+
const lifecycleHook = isDebugBrowserMode ? beforeEach : afterEach;
20+
lifecycleHook(() => getTestBed().resetTestingModule());
21+
22+
const testBed = getTestBed();
23+
if (testBed.platform) {
24+
return;
25+
}
26+
27+
@NgModule({ providers: [provideZonelessChangeDetection()] })
28+
class TestModule {}
29+
30+
testBed.initTestEnvironment(
31+
[BrowserTestingModule, TestModule],
32+
platformBrowserTesting(),
33+
{
34+
teardown: { destroyAfterEach: false },
35+
},
36+
);
37+
}

0 commit comments

Comments
 (0)