mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
1f8c53cd0c
Prior to this commit, `TestBed` would require tests call `flushEffects` or `fixture.detectChanges` in order to execute effects. In general, we want to discourage authoring tests like this because it makes the timing of change detection and effects differ from what happens in the application. Instead, developers should perform actions and `await` (or `flush`/`tick` when using `fakeAsync`) some `Promise` so that Angular can react to the changes in the same way that it does in the application. Note that this still _allows_ developers to flush effects synchronously with `flushEffects` and `detectChanges` but also enables the <action>, `await` pattern described above. PR Close #53843
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
import {PlatformLocation} from '@angular/common';
|
|
import {MockPlatformLocation} from '@angular/common/testing';
|
|
import {APP_ID, createPlatformFactory, NgModule, PLATFORM_INITIALIZER, platformCore, provideZoneChangeDetection, StaticProvider} from '@angular/core';
|
|
import {BrowserModule, ɵBrowserDomAdapter as BrowserDomAdapter} from '@angular/platform-browser';
|
|
|
|
function initBrowserTests() {
|
|
BrowserDomAdapter.makeCurrent();
|
|
}
|
|
|
|
const _TEST_BROWSER_PLATFORM_PROVIDERS: StaticProvider[] =
|
|
[{provide: PLATFORM_INITIALIZER, useValue: initBrowserTests, multi: true}];
|
|
|
|
/**
|
|
* Platform for testing
|
|
*
|
|
* @publicApi
|
|
*/
|
|
export const platformBrowserTesting =
|
|
createPlatformFactory(platformCore, 'browserTesting', _TEST_BROWSER_PLATFORM_PROVIDERS);
|
|
|
|
/**
|
|
* NgModule for testing.
|
|
*
|
|
* @publicApi
|
|
*/
|
|
@NgModule({
|
|
exports: [BrowserModule],
|
|
providers: [
|
|
{provide: APP_ID, useValue: 'a'},
|
|
provideZoneChangeDetection(),
|
|
{provide: PlatformLocation, useClass: MockPlatformLocation},
|
|
]
|
|
})
|
|
export class BrowserTestingModule {
|
|
}
|