Skip to content

Commit 9c29dd7

Browse files
alan-agius4dgp1130
authored andcommitted
test(@angular-devkit/build-angular): rewrite app-shell unit test into e2e
This particular unit tests was really an e2e test. (cherry picked from commit fdc315d)
1 parent dcbc5ac commit 9c29dd7

File tree

4 files changed

+63
-135
lines changed

4 files changed

+63
-135
lines changed

packages/angular_devkit/build_angular/BUILD.bazel

-7
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,7 @@ ts_library(
286286
LARGE_SPECS = {
287287
"app-shell": {
288288
"extra_deps": [
289-
"@npm//@angular/animations",
290289
"@npm//@angular/platform-server",
291-
"@npm//@types/express",
292-
"@npm//express",
293-
"@npm//jasmine-spec-reporter",
294-
"@npm//protractor",
295-
"@npm//puppeteer",
296-
"@npm//ts-node",
297290
],
298291
"tags": [
299292
# TODO: node crashes with an internal error on node16

packages/angular_devkit/build_angular/src/builders/app-shell/app-shell_spec.ts

+1-122
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { getSystemPath, join, normalize, virtualFs } from '@angular-devkit/core';
11-
import express from 'express'; // eslint-disable-line import/no-extraneous-dependencies
12-
import * as http from 'http';
13-
import { AddressInfo } from 'net';
10+
import { normalize, virtualFs } from '@angular-devkit/core';
1411
import { createArchitect, host } from '../../testing/test-utils';
1512

1613
describe('AppShell Builder', () => {
@@ -160,124 +157,6 @@ describe('AppShell Builder', () => {
160157
expect(content).toContain('app-shell works!');
161158
});
162159

163-
it('works with route and service-worker', async () => {
164-
host.writeMultipleFiles(appShellRouteFiles);
165-
host.writeMultipleFiles({
166-
'src/ngsw-config.json': `
167-
{
168-
"index": "/index.html",
169-
"assetGroups": [{
170-
"name": "app",
171-
"installMode": "prefetch",
172-
"resources": {
173-
"files": [
174-
"/favicon.ico",
175-
"/index.html",
176-
"/*.css",
177-
"/*.js"
178-
]
179-
}
180-
}, {
181-
"name": "assets",
182-
"installMode": "lazy",
183-
"updateMode": "prefetch",
184-
"resources": {
185-
"files": [
186-
"/assets/**"
187-
]
188-
}
189-
}]
190-
}
191-
`,
192-
'src/app/app.module.ts': `
193-
import { BrowserModule } from '@angular/platform-browser';
194-
import { NgModule } from '@angular/core';
195-
196-
import { AppRoutingModule } from './app-routing.module';
197-
import { AppComponent } from './app.component';
198-
import { ServiceWorkerModule } from '@angular/service-worker';
199-
import { environment } from '../environments/environment';
200-
import { RouterModule } from '@angular/router';
201-
202-
@NgModule({
203-
declarations: [
204-
AppComponent
205-
],
206-
imports: [
207-
BrowserModule.withServerTransition({ appId: 'serverApp' }),
208-
AppRoutingModule,
209-
ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production }),
210-
RouterModule
211-
],
212-
providers: [],
213-
bootstrap: [AppComponent]
214-
})
215-
export class AppModule { }
216-
`,
217-
'e2e/app.e2e-spec.ts': `
218-
import { browser, by, element } from 'protractor';
219-
220-
it('should have ngsw in normal state', () => {
221-
browser.get('/');
222-
// Wait for service worker to load.
223-
browser.sleep(2000);
224-
browser.waitForAngularEnabled(false);
225-
browser.get('/ngsw/state');
226-
// Should have updated, and be in normal state.
227-
expect(element(by.css('pre')).getText()).not.toContain('Last update check: never');
228-
expect(element(by.css('pre')).getText()).toContain('Driver state: NORMAL');
229-
});
230-
`,
231-
});
232-
// This should match the browser target prod config.
233-
host.replaceInFile(
234-
'angular.json',
235-
'"buildOptimizer": true',
236-
'"buildOptimizer": true, "serviceWorker": true',
237-
);
238-
239-
// We're changing the workspace file so we need to recreate the Architect instance.
240-
architect = (await createArchitect(host.root())).architect;
241-
242-
const overrides = { route: 'shell' };
243-
const run = await architect.scheduleTarget(
244-
{ ...target, configuration: 'production' },
245-
overrides,
246-
);
247-
const output = await run.result;
248-
await run.stop();
249-
250-
expect(output.success).toBe(true);
251-
252-
// Make sure the index is pre-rendering the route.
253-
const fileName = 'dist/index.html';
254-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
255-
expect(content).toContain('app-shell works!');
256-
257-
// Serve the app using a simple static server.
258-
const app = express();
259-
app.use('/', express.static(getSystemPath(join(host.root(), 'dist')) + '/'));
260-
const server = await new Promise<http.Server>((resolve) => {
261-
const innerServer = app.listen(0, 'localhost', () => resolve(innerServer));
262-
});
263-
try {
264-
const serverPort = (server.address() as AddressInfo).port;
265-
// Load app in protractor, then check service worker status.
266-
const protractorRun = await architect.scheduleTarget(
267-
{ project: 'app-e2e', target: 'e2e' },
268-
{ baseUrl: `http://localhost:${serverPort}/`, devServerTarget: '' },
269-
);
270-
271-
const protractorOutput = await protractorRun.result;
272-
await protractorRun.stop();
273-
274-
expect(protractorOutput.success).toBe(true);
275-
} finally {
276-
// Close the express server.
277-
await new Promise<void>((resolve) => server.close(() => resolve()));
278-
}
279-
});
280-
281160
it('critical CSS is inlined', async () => {
282161
host.writeMultipleFiles(appShellRouteFiles);
283162
const overrides = {

tests/legacy-cli/e2e/tests/build/build-app-shell-with-schematic.ts renamed to tests/legacy-cli/e2e/tests/build/app-shell/app-shell-with-schematic.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { getGlobalVariable } from '../../utils/env';
2-
import { appendToFile, expectFileToMatch } from '../../utils/fs';
3-
import { installPackage } from '../../utils/packages';
4-
import { ng } from '../../utils/process';
5-
import { updateJsonFile } from '../../utils/project';
1+
import { getGlobalVariable } from '../../../utils/env';
2+
import { appendToFile, expectFileToMatch } from '../../../utils/fs';
3+
import { installPackage } from '../../../utils/packages';
4+
import { ng } from '../../../utils/process';
5+
import { updateJsonFile } from '../../../utils/project';
66

7-
const snapshots = require('../../ng-snapshot/package.json');
7+
const snapshots = require('../../../ng-snapshot/package.json');
88

99
export default async function () {
1010
await appendToFile('src/app/app.component.html', '<router-outlet></router-outlet>');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { getGlobalVariable } from '../../../utils/env';
2+
import { appendToFile, expectFileToMatch, writeFile } from '../../../utils/fs';
3+
import { installPackage } from '../../../utils/packages';
4+
import { ng } from '../../../utils/process';
5+
import { updateJsonFile } from '../../../utils/project';
6+
7+
const snapshots = require('../../../ng-snapshot/package.json');
8+
9+
export default async function () {
10+
await appendToFile('src/app/app.component.html', '<router-outlet></router-outlet>');
11+
await ng('generate', 'service-worker', '--project', 'test-project');
12+
await ng('generate', 'app-shell', '--project', 'test-project');
13+
14+
const isSnapshotBuild = getGlobalVariable('argv')['ng-snapshots'];
15+
if (isSnapshotBuild) {
16+
const packagesToInstall: string[] = [];
17+
await updateJsonFile('package.json', (packageJson) => {
18+
const dependencies = packageJson['dependencies'];
19+
// Iterate over all of the packages to update them to the snapshot version.
20+
for (const [name, version] of Object.entries(
21+
snapshots.dependencies as { [p: string]: string },
22+
)) {
23+
if (name in dependencies && dependencies[name] !== version) {
24+
packagesToInstall.push(version);
25+
}
26+
}
27+
});
28+
29+
for (const pkg of packagesToInstall) {
30+
await installPackage(pkg);
31+
}
32+
}
33+
34+
await writeFile(
35+
'e2e/app.e2e-spec.ts',
36+
`
37+
import { browser, by, element } from 'protractor';
38+
39+
it('should have ngsw in normal state', () => {
40+
browser.get('/');
41+
// Wait for service worker to load.
42+
browser.sleep(2000);
43+
browser.waitForAngularEnabled(false);
44+
browser.get('/ngsw/state');
45+
// Should have updated, and be in normal state.
46+
expect(element(by.css('pre')).getText()).not.toContain('Last update check: never');
47+
expect(element(by.css('pre')).getText()).toContain('Driver state: NORMAL');
48+
});
49+
`,
50+
);
51+
52+
await ng('run', 'test-project:app-shell:production');
53+
await expectFileToMatch('dist/test-project/browser/index.html', /app-shell works!/);
54+
55+
await ng('e2e', '--configuration=production');
56+
}

0 commit comments

Comments
 (0)