Skip to content

Commit 7774fbe

Browse files
committed
test: migrate app-shell-with-service-worker E2E test to Puppeteer
Replaces the Protractor-based ng e2e execution with the new Puppeteer executeBrowserTest utility in `build/app-shell/app-shell-with-service-worker.ts` E2E test.
1 parent 164e7db commit 7774fbe

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

tests/e2e/tests/build/app-shell/app-shell-with-service-worker.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { setTimeout } from 'node:timers/promises';
12
import { getGlobalVariable } from '../../../utils/env';
2-
import { appendToFile, expectFileToMatch, writeFile } from '../../../utils/fs';
3+
import { appendToFile, expectFileToMatch } from '../../../utils/fs';
34
import { installPackage } from '../../../utils/packages';
45
import { ng } from '../../../utils/process';
56
import { updateJsonFile } from '../../../utils/project';
7+
import { executeBrowserTest } from '../../../utils/puppeteer';
68

79
const snapshots = require('../../../ng-snapshot/package.json');
810

@@ -31,26 +33,26 @@ export default async function () {
3133
}
3234
}
3335

34-
await writeFile(
35-
'e2e/app.e2e-spec.ts',
36-
`
37-
import { browser, by, element } from 'protractor';
36+
await ng('build');
37+
await expectFileToMatch('dist/test-project/browser/index.html', /app-shell works!/);
3838

39-
it('should have ngsw in normal state', () => {
40-
browser.get('/');
39+
await executeBrowserTest({
40+
configuration: 'production',
41+
checkFn: async (page) => {
4142
// 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-
);
43+
await setTimeout(2000);
5144

52-
await ng('build');
53-
await expectFileToMatch('dist/test-project/browser/index.html', /app-shell works!/);
45+
const baseUrl = page.url();
46+
await page.goto(new URL('/ngsw/state', baseUrl).href);
5447

55-
await ng('e2e', '--configuration=production');
48+
// Should have updated, and be in normal state.
49+
const preText = await page.$eval('pre', (el) => el.textContent);
50+
if (preText?.includes('Last update check: never')) {
51+
throw new Error(`Expected service worker to have checked for updates, but got: ${preText}`);
52+
}
53+
if (!preText?.includes('Driver state: NORMAL')) {
54+
throw new Error(`Expected service worker driver state to be NORMAL, but got: ${preText}`);
55+
}
56+
},
57+
});
5658
}

tests/e2e/utils/puppeteer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function executeBrowserTest(options: BrowserTestOptions = {}) {
1818
if (!url) {
1919
// Start serving and find address (1 - Webpack; 2 - Vite)
2020
const match = /(?:open your browser on|Local:)\s+(http:\/\/localhost:\d+\/)/;
21-
const serveArgs = ['serve', '--port=0'];
21+
const serveArgs = ['serve', '--port=0', '--no-watch', '--no-live-reload'];
2222
if (options.project) {
2323
serveArgs.push(options.project);
2424
}

0 commit comments

Comments
 (0)