Skip to content

Commit 1327ee3

Browse files
clydinmgechev
authored andcommitted
test(@angular-devkit/build-angular): update service worker browser tests to stable API
1 parent 61717b7 commit 1327ee3

File tree

1 file changed

+110
-104
lines changed

1 file changed

+110
-104
lines changed

packages/angular_devkit/build_angular/test/browser/service-worker_spec_large.ts

+110-104
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
9-
import { runTargetSpec } from '@angular-devkit/architect/testing';
8+
import { Architect } from '@angular-devkit/architect/src/index2';
109
import { normalize, virtualFs } from '@angular-devkit/core';
11-
import { tap } from 'rxjs/operators';
12-
import { browserTargetSpec, host } from '../utils';
13-
10+
import { createArchitect, host } from '../utils';
1411

1512
describe('Browser Builder service worker', () => {
1613
const manifest = {
@@ -43,128 +40,137 @@ describe('Browser Builder service worker', () => {
4340
],
4441
};
4542

46-
beforeEach(done => host.initialize().toPromise().then(done, done.fail));
47-
afterEach(done => host.restore().toPromise().then(done, done.fail));
43+
const target = { project: 'app', target: 'build' };
44+
let architect: Architect;
4845

49-
it('errors if no ngsw-config.json is present', (done) => {
46+
beforeEach(async () => {
47+
await host.initialize().toPromise();
48+
architect = (await createArchitect(host.root())).architect;
49+
});
50+
afterEach(async () => host.restore().toPromise());
51+
52+
it('errors if no ngsw-config.json is present', async () => {
5053
const overrides = { serviceWorker: true };
5154

52-
runTargetSpec(host, browserTargetSpec, overrides)
53-
.subscribe(event => {
54-
expect(event.success).toBe(false);
55-
}, () => done(), done.fail);
55+
const run = await architect.scheduleTarget(target, overrides);
56+
57+
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: false }));
58+
59+
await run.stop();
5660
});
5761

58-
it('works with service worker', (done) => {
62+
it('works with service worker', async () => {
5963
host.writeMultipleFiles({
6064
'src/ngsw-config.json': JSON.stringify(manifest),
6165
'src/assets/folder-asset.txt': 'folder-asset.txt',
6266
'src/styles.css': `body { background: url(./spectrum.png); }`,
6367
});
6468

6569
const overrides = { serviceWorker: true };
66-
runTargetSpec(host, browserTargetSpec, overrides).pipe(
67-
tap(buildEvent => {
68-
expect(buildEvent.success).toBe(true);
69-
expect(host.scopedSync().exists(normalize('dist/ngsw.json')));
70-
const ngswJson = JSON.parse(virtualFs.fileBufferToString(
71-
host.scopedSync().read(normalize('dist/ngsw.json'))));
72-
// Verify index and assets are there.
73-
expect(ngswJson).toEqual(jasmine.objectContaining({
74-
configVersion: 1,
75-
index: '/index.html',
76-
navigationUrls: [
77-
{ positive: true, regex: '^\\\/.*$' },
78-
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*\\.[^\/]*$' },
79-
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*__[^\/]*$' },
80-
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*__[^\/]*\\\/.*$' },
70+
const run = await architect.scheduleTarget(target, overrides);
71+
72+
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
73+
74+
expect(host.scopedSync().exists(normalize('dist/ngsw.json')));
75+
const ngswJson = JSON.parse(virtualFs.fileBufferToString(
76+
host.scopedSync().read(normalize('dist/ngsw.json'))));
77+
// Verify index and assets are there.
78+
expect(ngswJson).toEqual(jasmine.objectContaining({
79+
configVersion: 1,
80+
index: '/index.html',
81+
navigationUrls: [
82+
{ positive: true, regex: '^\\\/.*$' },
83+
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*\\.[^\/]*$' },
84+
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*__[^\/]*$' },
85+
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*__[^\/]*\\\/.*$' },
86+
],
87+
assetGroups: [
88+
{
89+
name: 'app',
90+
installMode: 'prefetch',
91+
updateMode: 'prefetch',
92+
urls: [
93+
'/favicon.ico',
94+
'/index.html',
8195
],
82-
assetGroups: [
83-
{
84-
name: 'app',
85-
installMode: 'prefetch',
86-
updateMode: 'prefetch',
87-
urls: [
88-
'/favicon.ico',
89-
'/index.html',
90-
],
91-
patterns: [],
92-
},
93-
{
94-
name: 'assets',
95-
installMode: 'lazy',
96-
updateMode: 'prefetch',
97-
urls: [
98-
'/assets/folder-asset.txt',
99-
'/spectrum.png',
100-
],
101-
patterns: [],
102-
},
96+
patterns: [],
97+
},
98+
{
99+
name: 'assets',
100+
installMode: 'lazy',
101+
updateMode: 'prefetch',
102+
urls: [
103+
'/assets/folder-asset.txt',
104+
'/spectrum.png',
103105
],
104-
dataGroups: [],
105-
hashTable: {
106-
'/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
107-
'/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
108-
'/index.html': '1bcafd53046ffb270ac5e6f3cab23e0442f95c4f',
109-
'/spectrum.png': '8d048ece46c0f3af4b598a95fd8e4709b631c3c0',
110-
},
111-
}));
112-
}),
113-
).toPromise().then(done, done.fail);
106+
patterns: [],
107+
},
108+
],
109+
dataGroups: [],
110+
hashTable: {
111+
'/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
112+
'/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
113+
'/index.html': '1bcafd53046ffb270ac5e6f3cab23e0442f95c4f',
114+
'/spectrum.png': '8d048ece46c0f3af4b598a95fd8e4709b631c3c0',
115+
},
116+
}));
117+
118+
await run.stop();
114119
});
115120

116-
it('works with service worker and baseHref', (done) => {
121+
it('works with service worker and baseHref', async () => {
117122
host.writeMultipleFiles({
118123
'src/ngsw-config.json': JSON.stringify(manifest),
119124
'src/assets/folder-asset.txt': 'folder-asset.txt',
120125
});
121126

122127
const overrides = { serviceWorker: true, baseHref: '/foo/bar' };
123-
runTargetSpec(host, browserTargetSpec, overrides).pipe(
124-
tap(buildEvent => {
125-
expect(buildEvent.success).toBe(true);
126-
expect(host.scopedSync().exists(normalize('dist/ngsw.json')));
127-
const ngswJson = JSON.parse(virtualFs.fileBufferToString(
128-
host.scopedSync().read(normalize('dist/ngsw.json'))));
129-
// Verify index and assets include the base href.
130-
expect(ngswJson).toEqual(jasmine.objectContaining({
131-
configVersion: 1,
132-
index: '/foo/bar/index.html',
133-
navigationUrls: [
134-
{ positive: true, regex: '^\\\/.*$' },
135-
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*\\.[^\/]*$' },
136-
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*__[^\/]*$' },
137-
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*__[^\/]*\\\/.*$' },
128+
const run = await architect.scheduleTarget(target, overrides);
129+
130+
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
131+
132+
expect(host.scopedSync().exists(normalize('dist/ngsw.json')));
133+
const ngswJson = JSON.parse(virtualFs.fileBufferToString(
134+
host.scopedSync().read(normalize('dist/ngsw.json'))));
135+
// Verify index and assets include the base href.
136+
expect(ngswJson).toEqual(jasmine.objectContaining({
137+
configVersion: 1,
138+
index: '/foo/bar/index.html',
139+
navigationUrls: [
140+
{ positive: true, regex: '^\\\/.*$' },
141+
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*\\.[^\/]*$' },
142+
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*__[^\/]*$' },
143+
{ positive: false, regex: '^\\\/(?:.+\\\/)?[^\/]*__[^\/]*\\\/.*$' },
144+
],
145+
assetGroups: [
146+
{
147+
name: 'app',
148+
installMode: 'prefetch',
149+
updateMode: 'prefetch',
150+
urls: [
151+
'/foo/bar/favicon.ico',
152+
'/foo/bar/index.html',
138153
],
139-
assetGroups: [
140-
{
141-
name: 'app',
142-
installMode: 'prefetch',
143-
updateMode: 'prefetch',
144-
urls: [
145-
'/foo/bar/favicon.ico',
146-
'/foo/bar/index.html',
147-
],
148-
patterns: [],
149-
},
150-
{
151-
name: 'assets',
152-
installMode: 'lazy',
153-
updateMode: 'prefetch',
154-
urls: [
155-
'/foo/bar/assets/folder-asset.txt',
156-
],
157-
patterns: [],
158-
},
154+
patterns: [],
155+
},
156+
{
157+
name: 'assets',
158+
installMode: 'lazy',
159+
updateMode: 'prefetch',
160+
urls: [
161+
'/foo/bar/assets/folder-asset.txt',
159162
],
160-
dataGroups: [],
161-
hashTable: {
162-
'/foo/bar/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
163-
'/foo/bar/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
164-
'/foo/bar/index.html': '925d80777b6ba64b526b0be79761d254dfe94c65',
165-
},
166-
}));
167-
}),
168-
).toPromise().then(done, done.fail);
163+
patterns: [],
164+
},
165+
],
166+
dataGroups: [],
167+
hashTable: {
168+
'/foo/bar/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
169+
'/foo/bar/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
170+
'/foo/bar/index.html': '925d80777b6ba64b526b0be79761d254dfe94c65',
171+
},
172+
}));
173+
174+
await run.stop();
169175
});
170176
});

0 commit comments

Comments
 (0)