Skip to content

Commit 3165fa3

Browse files
committed
perf(platform-browser): avoid including Testability by default in bootstrapApplication (angular#45885)
The Testability-related logic was refactored in angular#45657 to become tree-shaking-friendly: it was decoupled from the core providers of the `BrowserModule`. This commit updates the newly-introduced `bootstrapApplication` function to exclude Testability-providers by default (note: the Testability is still included in the NgModule-based bootstrap). In order to add the Testability to the app bootstrapped via `bootstrapApplication`, the `provideProtractorTestingSupport` function is introduced. PR Close angular#45885
1 parent e441ff4 commit 3165fa3

File tree

7 files changed

+60
-53
lines changed

7 files changed

+60
-53
lines changed

goldens/public-api/platform-browser/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ export type MetaDefinition = {
177177
// @public
178178
export const platformBrowser: (extraProviders?: StaticProvider[]) => PlatformRef;
179179

180+
// @public
181+
export function provideProtractorTestingSupport(): Provider[];
182+
180183
// @public
181184
export interface SafeHtml extends SafeValue {
182185
}

goldens/size-tracking/integration-payloads.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"standalone-bootstrap": {
5656
"uncompressed": {
5757
"runtime": 1090,
58-
"main": 87940,
58+
"main": 84237,
5959
"polyfills": 33945
6060
}
6161
},
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
1-
import {browser, logging} from 'protractor';
2-
3-
import {AppPage} from './app.po';
4-
1+
/**
2+
* Note: this file contains no e2e tests, since they rely on Protractor,
3+
* which requires Testability. Testability is not included by default when
4+
* the `bootstrapApplication` function is used (which is the case in this app).
5+
* We use this app primarily to measure payload size, so we want to keep
6+
* Testability excluded.
7+
*/
58
describe('Standalone Bootstrap app', () => {
6-
let page: AppPage;
7-
8-
beforeEach(() => {
9-
page = new AppPage();
10-
});
11-
12-
it('should display title', () => {
13-
page.navigateTo();
14-
expect(page.getTitleText()).toEqual('Standalone Bootstrap app');
15-
});
16-
17-
afterEach(async () => {
18-
// Assert that there are no errors emitted from the browser
19-
const logs = await browser.manage().logs().get(logging.Type.BROWSER);
20-
expect(logs).not.toContain(jasmine.objectContaining({
21-
level: logging.Level.SEVERE,
22-
} as logging.Entry));
23-
});
9+
// Jasmine will throw if there are no tests.
10+
it('should pass', () => {});
2411
});

packages/core/src/testability/testability.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,22 @@ export const TESTABILITY_GETTER = new InjectionToken<GetTestability>('');
6666

6767
/**
6868
* The Testability service provides testing hooks that can be accessed from
69-
* the browser. Each bootstrapped Angular application on the page will have
70-
* an instance of Testability.
69+
* the browser.
70+
*
71+
* Angular applications bootstrapped using an NgModule (via `@NgModule.bootstrap` field) will also
72+
* instantiate Testability by default (in both development and production modes).
73+
*
74+
* For applications bootstrapped using the `bootstrapApplication` function, Testability is not
75+
* included by default. You can include it into your applications by getting the list of necessary
76+
* providers using the `provideProtractorTestingSupport()` function and adding them into the
77+
* `options.providers` array. Example:
78+
*
79+
* ```typescript
80+
* import {provideProtractorTestingSupport} from '@angular/platform-browser';
81+
*
82+
* await bootstrapApplication(RootComponent, providers: [provideProtractorTestingSupport()]);
83+
* ```
84+
*
7185
* @publicApi
7286
*/
7387
@Injectable()

packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@
6868
{
6969
"name": "DefaultDomRenderer2"
7070
},
71-
{
72-
"name": "DomEventsPlugin"
73-
},
7471
{
7572
"name": "DomRendererFactory2"
7673
},
@@ -137,9 +134,6 @@
137134
{
138135
"name": "Injector"
139136
},
140-
{
141-
"name": "KeyEventsPlugin"
142-
},
143137
{
144138
"name": "LOCALE_ID2"
145139
},
@@ -296,24 +290,12 @@
296290
{
297291
"name": "TESTABILITY"
298292
},
299-
{
300-
"name": "TESTABILITY_GETTER"
301-
},
302-
{
303-
"name": "TESTABILITY_PROVIDERS"
304-
},
305293
{
306294
"name": "THROW_IF_NOT_FOUND"
307295
},
308296
{
309297
"name": "TRACKED_LVIEWS"
310298
},
311-
{
312-
"name": "Testability"
313-
},
314-
{
315-
"name": "TestabilityRegistry"
316-
},
317299
{
318300
"name": "USE_VALUE"
319301
},
@@ -362,9 +344,6 @@
362344
{
363345
"name": "_renderCompCount"
364346
},
365-
{
366-
"name": "_testabilityGetter"
367-
},
368347
{
369348
"name": "_wrapInTimeout"
370349
},
@@ -815,9 +794,6 @@
815794
{
816795
"name": "scheduleArray"
817796
},
818-
{
819-
"name": "scheduleMicroTask"
820-
},
821797
{
822798
"name": "searchTokensOnInjector"
823799
},

packages/platform-browser/src/browser.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ export interface ApplicationConfig {
4949
* const appRef: ApplicationRef = await bootstrapApplication(RootComponent);
5050
* ```
5151
*
52+
* Note: this bootstrap method doesn't include [Testability](api/core/Testability) by default.
53+
* You can add [Testability](api/core/Testability) by getting the list of necessary providers
54+
* using `provideProtractorTestingSupport()` function and add them into the `options.providers`
55+
* array. Example:
56+
*
57+
* ```typescript
58+
* import {provideProtractorTestingSupport} from '@angular/platform-browser';
59+
*
60+
* await bootstrapApplication(RootComponent, providers: [provideProtractorTestingSupport()]);
61+
* ```
62+
*
5263
* @param rootComponent A reference to a Standalone Component that should be rendered.
5364
* @param options Additional configuration for the bootstrap operation, see `ApplicationConfig` for
5465
* additional info.
@@ -62,13 +73,29 @@ export function bootstrapApplication(
6273
rootComponent,
6374
appProviders: [
6475
...BROWSER_MODULE_PROVIDERS,
65-
...TESTABILITY_PROVIDERS,
6676
...(options?.providers ?? []),
6777
],
6878
platformProviders: INTERNAL_BROWSER_PLATFORM_PROVIDERS,
6979
});
7080
}
7181

82+
/**
83+
* Returns a set of providers required to setup [Testability](api/core/Testability) for an
84+
* application bootstrapped using the `bootstrapApplication` function. The set of providers is
85+
* needed to support testing an application with Protractor (which relies on the Testability APIs
86+
* to be present).
87+
*
88+
* @returns An array of providers required to setup Testability for an application and make it
89+
* available for testing using Protractor.
90+
*
91+
* @publicApi
92+
*/
93+
export function provideProtractorTestingSupport(): Provider[] {
94+
// Return a copy to prevent changes to the original array in case any in-place
95+
// alterations are performed to the `provideProtractorTestingSupport` call results in app code.
96+
return [...TESTABILITY_PROVIDERS];
97+
}
98+
7299
export function initDomAdapter() {
73100
BrowserDomAdapter.makeCurrent();
74101
}
@@ -107,7 +134,7 @@ export const platformBrowser: (extraProviders?: StaticProvider[]) => PlatformRef
107134
const BROWSER_MODULE_PROVIDERS_MARKER =
108135
new InjectionToken(NG_DEV_MODE ? 'BrowserModule Providers Marker' : '');
109136

110-
export const TESTABILITY_PROVIDERS = [
137+
const TESTABILITY_PROVIDERS = [
111138
{
112139
provide: TESTABILITY_GETTER,
113140
useClass: BrowserGetTestability,
@@ -125,7 +152,7 @@ export const TESTABILITY_PROVIDERS = [
125152
}
126153
];
127154

128-
export const BROWSER_MODULE_PROVIDERS: StaticProvider[] = [
155+
const BROWSER_MODULE_PROVIDERS: Provider[] = [
129156
{provide: INJECTOR_SCOPE, useValue: 'root'},
130157
{provide: ErrorHandler, useFactory: errorHandler, deps: []}, {
131158
provide: EVENT_MANAGER_PLUGINS,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
export {ApplicationConfig, bootstrapApplication, BrowserModule, platformBrowser} from './browser';
9+
export {ApplicationConfig, bootstrapApplication, BrowserModule, platformBrowser, provideProtractorTestingSupport} from './browser';
1010
export {Meta, MetaDefinition} from './browser/meta';
1111
export {Title} from './browser/title';
1212
export {disableDebugTools, enableDebugTools} from './browser/tools/tools';

0 commit comments

Comments
 (0)