-
-
Notifications
You must be signed in to change notification settings - Fork 298
fix(vitest-angular): reset TestBed between tests in Vitest Browser Mode
#1995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| If TestBed is not reset between tests, the following error would be thrown on second test of the second test file: | ||
|
|
||
| > Error: Cannot configure the test module when the test module has already been instantiated. | ||
| > Make sure you are not using `inject` before `TestBed.configureTestingModule`. | ||
| That is why we have two test files: | ||
|
|
||
| - `reset-test-bed-between-tests-1.spec.ts` | ||
| - `reset-test-bed-between-tests-2.spec.ts` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { InjectionToken } from '@angular/core'; | ||
| import { TestBed } from '@angular/core/testing'; | ||
| import { expect, test } from 'vitest'; | ||
|
|
||
| test.each([1, 2])('provide and inject dependency #%s', () => { | ||
| const MY_TOKEN = new InjectionToken<string>('MY_TOKEN'); | ||
|
|
||
| TestBed.configureTestingModule({ | ||
| providers: [{ provide: MY_TOKEN, useValue: 'My Value' }], | ||
| }); | ||
|
|
||
| expect(TestBed.inject(MY_TOKEN)).toBe('My Value'); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { InjectionToken } from '@angular/core'; | ||
| import { TestBed } from '@angular/core/testing'; | ||
| import { expect, test } from 'vitest'; | ||
|
|
||
| test.each([1, 2])('provide and inject dependency #%s', () => { | ||
| const MY_TOKEN = new InjectionToken<string>('MY_TOKEN'); | ||
|
|
||
| TestBed.configureTestingModule({ | ||
| providers: [{ provide: MY_TOKEN, useValue: 'My Value' }], | ||
| }); | ||
|
|
||
| expect(TestBed.inject(MY_TOKEN)).toBe('My Value'); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,3 @@ | ||
| import '@analogjs/vitest-angular/setup-zone'; | ||
| import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed'; | ||
|
|
||
| /** | ||
| * Initialize TestBed for all tests inside of content | ||
| */ | ||
| import { TestBed } from '@angular/core/testing'; | ||
| import { | ||
| BrowserDynamicTestingModule, | ||
| platformBrowserDynamicTesting, | ||
| } from '@angular/platform-browser-dynamic/testing'; | ||
|
|
||
| TestBed.initTestEnvironment( | ||
| BrowserDynamicTestingModule, | ||
| platformBrowserDynamicTesting(), | ||
| ); | ||
| setupTestBed(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,6 @@ import { | |
| } from '@angular/platform-browser/testing'; | ||
| import { afterEach, beforeEach } from 'vitest'; | ||
|
|
||
| beforeEach(getCleanupHook(false)); | ||
| afterEach(getCleanupHook(true)); | ||
|
|
||
| const ANGULAR_TESTBED_SETUP = Symbol.for('testbed-setup'); | ||
|
|
||
| type TestBedSetupOptions = { | ||
|
|
@@ -22,6 +19,9 @@ type TestBedSetupOptions = { | |
| export function setupTestBed( | ||
| options: TestBedSetupOptions = { zoneless: true, providers: [] }, | ||
| ) { | ||
| beforeEach(getCleanupHook(false)); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, we're sure the hooks will be registered.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you see any tests slow down by putting them here? Or does it have no impact with the Vitest fixes
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No slowdown observed, and it shouldn't slow down anything. |
||
| afterEach(getCleanupHook(true)); | ||
|
|
||
| if (!(globalThis as any)[ANGULAR_TESTBED_SETUP]) { | ||
| (globalThis as any)[ANGULAR_TESTBED_SETUP] = true; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.