6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { DefaultTimeout , TestLogger , runTargetSpec } from '@angular-devkit/architect/testing' ;
9
+ import { Architect } from '@angular-devkit/architect/src/index2' ;
10
+ import { TestLogger } from '@angular-devkit/architect/testing' ;
10
11
import { join , virtualFs } from '@angular-devkit/core' ;
11
12
import { debounceTime , takeWhile , tap } from 'rxjs/operators' ;
12
- import { browserTargetSpec , host , outputPath } from '../utils' ;
13
+ import { browserBuild , createArchitect , host , outputPath } from '../utils' ;
13
14
14
15
15
16
describe ( 'Browser Builder Web Worker support' , ( ) => {
16
- beforeEach ( done => host . initialize ( ) . toPromise ( ) . then ( done , done . fail ) ) ;
17
- afterEach ( done => host . restore ( ) . toPromise ( ) . then ( done , done . fail ) ) ;
17
+ const target = { project : 'app' , target : 'build' } ;
18
+ let architect : Architect ;
19
+
20
+ beforeEach ( async ( ) => {
21
+ await host . initialize ( ) . toPromise ( ) ;
22
+ architect = ( await createArchitect ( host . root ( ) ) ) . architect ;
23
+ } ) ;
24
+ afterEach ( async ( ) => host . restore ( ) . toPromise ( ) ) ;
18
25
19
26
const workerFiles : { [ k : string ] : string } = {
20
27
'src/app/dep.ts' : `export const foo = 'bar';` ,
@@ -77,62 +84,52 @@ describe('Browser Builder Web Worker support', () => {
77
84
}` ,
78
85
} ;
79
86
80
- it ( 'bundles TS worker' , ( done ) => {
81
- const logger = new TestLogger ( 'worker-warnings' ) ;
87
+ it ( 'bundles TS worker' , async ( ) => {
82
88
host . writeMultipleFiles ( workerFiles ) ;
89
+ const logger = new TestLogger ( 'worker-warnings' ) ;
83
90
const overrides = { webWorkerTsConfig : 'src/tsconfig.worker.json' } ;
84
- runTargetSpec ( host , browserTargetSpec , overrides , DefaultTimeout , logger ) . pipe (
85
- tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
86
- tap ( ( ) => {
87
- const workerContent = virtualFs . fileBufferToString (
88
- host . scopedSync ( ) . read ( join ( outputPath , '0.worker.js' ) ) ,
89
- ) ;
90
- // worker bundle contains worker code.
91
- expect ( workerContent ) . toContain ( 'hello from worker' ) ;
92
- expect ( workerContent ) . toContain ( 'bar' ) ;
93
-
94
- const mainContent = virtualFs . fileBufferToString (
95
- host . scopedSync ( ) . read ( join ( outputPath , 'main.js' ) ) ,
96
- ) ;
97
- // main bundle references worker.
98
- expect ( mainContent ) . toContain ( '0.worker.js' ) ;
99
- } ) ,
100
- // Doesn't show any warnings.
101
- tap ( ( ) => expect ( logger . includes ( 'WARNING' ) ) . toBe ( false , 'Should show no warnings.' ) ) ,
102
- ) . toPromise ( ) . then ( done , done . fail ) ;
91
+ await browserBuild ( architect , host , target , overrides , { logger } ) ;
92
+
93
+ // Worker bundle contains worker code.
94
+ const workerContent = virtualFs . fileBufferToString (
95
+ host . scopedSync ( ) . read ( join ( outputPath , '0.worker.js' ) ) ) ;
96
+ expect ( workerContent ) . toContain ( 'hello from worker' ) ;
97
+ expect ( workerContent ) . toContain ( 'bar' ) ;
98
+
99
+ // Main bundle references worker.
100
+ const mainContent = virtualFs . fileBufferToString (
101
+ host . scopedSync ( ) . read ( join ( outputPath , 'main.js' ) ) ) ;
102
+ expect ( mainContent ) . toContain ( '0.worker.js' ) ;
103
+ expect ( logger . includes ( 'WARNING' ) ) . toBe ( false , 'Should show no warnings.' ) ;
103
104
} ) ;
104
105
105
- it ( 'minimizes and hashes worker' , ( done ) => {
106
+ it ( 'minimizes and hashes worker' , async ( ) => {
106
107
host . writeMultipleFiles ( workerFiles ) ;
107
108
const overrides = {
108
109
webWorkerTsConfig : 'src/tsconfig.worker.json' ,
109
110
outputHashing : 'all' ,
110
111
optimization : true ,
111
112
} ;
112
- runTargetSpec ( host , browserTargetSpec , overrides ) . pipe (
113
- tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
114
- tap ( ( ) => {
115
- const workerBundle = host . fileMatchExists ( outputPath ,
116
- / 0 \. [ 0 - 9 a - f ] { 20 } \. w o r k e r \. j s / ) as string ;
117
- expect ( workerBundle ) . toBeTruthy ( 'workerBundle should exist' ) ;
118
- const workerContent = virtualFs . fileBufferToString (
119
- host . scopedSync ( ) . read ( join ( outputPath , workerBundle ) ) ,
120
- ) ;
121
- expect ( workerContent ) . toContain ( 'hello from worker' ) ;
122
- expect ( workerContent ) . toContain ( 'bar' ) ;
123
- expect ( workerContent ) . toContain ( '"hello"===t&&postMessage' ) ;
124
-
125
- const mainBundle = host . fileMatchExists ( outputPath , / m a i n \. [ 0 - 9 a - f ] { 20 } \. j s / ) as string ;
126
- expect ( mainBundle ) . toBeTruthy ( 'mainBundle should exist' ) ;
127
- const mainContent = virtualFs . fileBufferToString (
128
- host . scopedSync ( ) . read ( join ( outputPath , mainBundle ) ) ,
129
- ) ;
130
- expect ( mainContent ) . toContain ( workerBundle ) ;
131
- } ) ,
132
- ) . toPromise ( ) . then ( done , done . fail ) ;
113
+ await browserBuild ( architect , host , target , overrides ) ;
114
+
115
+ // Worker bundle should have hash and minified code.
116
+ const workerBundle = host . fileMatchExists ( outputPath , / 0 \. [ 0 - 9 a - f ] { 20 } \. w o r k e r \. j s / ) as string ;
117
+ expect ( workerBundle ) . toBeTruthy ( 'workerBundle should exist' ) ;
118
+ const workerContent = virtualFs . fileBufferToString (
119
+ host . scopedSync ( ) . read ( join ( outputPath , workerBundle ) ) ) ;
120
+ expect ( workerContent ) . toContain ( 'hello from worker' ) ;
121
+ expect ( workerContent ) . toContain ( 'bar' ) ;
122
+ expect ( workerContent ) . toContain ( '"hello"===t&&postMessage' ) ;
123
+
124
+ // Main bundle should reference hashed worker bundle.
125
+ const mainBundle = host . fileMatchExists ( outputPath , / m a i n \. [ 0 - 9 a - f ] { 20 } \. j s / ) as string ;
126
+ expect ( mainBundle ) . toBeTruthy ( 'mainBundle should exist' ) ;
127
+ const mainContent = virtualFs . fileBufferToString (
128
+ host . scopedSync ( ) . read ( join ( outputPath , mainBundle ) ) ) ;
129
+ expect ( mainContent ) . toContain ( workerBundle ) ;
133
130
} ) ;
134
131
135
- it ( 'rebuilds TS worker' , ( done ) => {
132
+ it ( 'rebuilds TS worker' , async ( ) => {
136
133
host . writeMultipleFiles ( workerFiles ) ;
137
134
const overrides = {
138
135
webWorkerTsConfig : 'src/tsconfig.worker.json' ,
@@ -144,7 +141,8 @@ describe('Browser Builder Web Worker support', () => {
144
141
const workerPath = join ( outputPath , '0.worker.js' ) ;
145
142
let workerContent = '' ;
146
143
147
- runTargetSpec ( host , browserTargetSpec , overrides , DefaultTimeout * 3 ) . pipe (
144
+ const run = await architect . scheduleTarget ( target , overrides ) ;
145
+ await run . output . pipe (
148
146
// Wait for files to be written to disk.
149
147
debounceTime ( 1000 ) ,
150
148
tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true , 'build should succeed' ) ) ,
@@ -178,9 +176,7 @@ describe('Browser Builder Web Worker support', () => {
178
176
}
179
177
} ) ,
180
178
takeWhile ( ( ) => phase < 3 ) ,
181
- ) . toPromise ( ) . then (
182
- ( ) => done ( ) ,
183
- ( ) => done . fail ( `stuck at phase ${ phase } [builds: ${ buildCount } ]` ) ,
184
- ) ;
179
+ ) . toPromise ( ) ;
180
+ await run . stop ( ) ;
185
181
} ) ;
186
182
} ) ;
0 commit comments