Skip to content

Commit 5dce2a5

Browse files
atscottalxhub
authored andcommitted
feat(common): Provide MockPlatformLocation by default in BrowserTestingModule (#49137)
Tests sometimes do not mock out the `PlatformLocation` and end up affecting real browser state. This can mean changing the real URL of the browser during a test, updating the History state object, or any number of other stateful operations. This can result in a test unintentionally affecting other tests in the suite because the browser state does not usually get reset before the next test runs. Providing `MockPlatformLocation` by default prevents these types of accidental test leakages. In addition, not providing `MockPlatformLocation` by default led to developers needing to add `RouterTestingModule` to their test suite to avoid the problems above. This module has spy `Location` providers which prevent those issues. This commit now makes `RouterTestingModule` obsolete. Developers can now just use `RouterModule.forRoot` or `provideRouter` directly in tests _without_ needing to learn to import additional test providers or modules. With this, we should consider deprecating `RouterTestingModule` altogether and migrating developers to `RouterModule.forRoot` or `provideRouter` instead. There are some small differences between `SpyLocation` and `MockPlatformLocation` that might cause tests to fail after the migration (`MockPlatformLocation` is actually more correct in its behaviors). If this happens, we can advise developers to also add `provideLocationMocks()` to their test providers, which would re-provide the `SpyLocation` like before and should make the tests pass again. BREAKING CHANGE: `MockPlatformLocation` is now provided by default in tests. Existing tests may have behaviors which rely on `BrowserPlatformLocation` instead. For example, direct access to the `window.history` in either the test or the component rather than going through the Angular APIs (`Location.getState()`). The quickest fix is to update the providers in the test suite to override the provider again `TestBed.configureTestingModule({providers: [{provide: PlatformLocation, useClass: BrowserPlatformLocation}]})`. The ideal fix would be to update the code to instead be compatible with `MockPlatformLocation` instead. PR Close #49137
1 parent 1e32709 commit 5dce2a5

File tree

2 files changed

+1
-20
lines changed

2 files changed

+1
-20
lines changed

packages/platform-browser/testing/src/browser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {APP_ID, createPlatformFactory, NgModule, NgZone, PLATFORM_INITIALIZER, p
1111
import {BrowserModule, ɵBrowserDomAdapter as BrowserDomAdapter} from '@angular/platform-browser';
1212

1313
import {BrowserDetection, createNgZone} from './browser_util';
14-
import {ENABLE_MOCK_PLATFORM_LOCATION} from './mock_platform_location_flag';
1514

1615
function initBrowserTests() {
1716
BrowserDomAdapter.makeCurrent();
@@ -39,8 +38,7 @@ export const platformBrowserTesting =
3938
providers: [
4039
{provide: APP_ID, useValue: 'a'},
4140
{provide: NgZone, useFactory: createNgZone},
42-
(ENABLE_MOCK_PLATFORM_LOCATION ? [{provide: PlatformLocation, useClass: MockPlatformLocation}] :
43-
[]),
41+
{provide: PlatformLocation, useClass: MockPlatformLocation},
4442
]
4543
})
4644
export class BrowserTestingModule {

packages/platform-browser/testing/src/mock_platform_location_flag.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)