9
9
import { Architect } from '@angular-devkit/architect' ;
10
10
import { TestLogger } from '@angular-devkit/architect/testing' ;
11
11
import { take , tap , timeout } from 'rxjs/operators' ;
12
- import { browserBuild , createArchitect , host , lazyModuleFiles , lazyModuleImport } from '../utils' ;
12
+ import {
13
+ browserBuild , createArchitect , host , lazyModuleFiles ,
14
+ lazyModuleFnImport , lazyModuleStringImport ,
15
+ } from '../utils' ;
13
16
14
17
// tslint:disable-next-line:no-big-function
15
18
describe ( 'Browser Builder lazy modules' , ( ) => {
@@ -22,46 +25,60 @@ describe('Browser Builder lazy modules', () => {
22
25
} ) ;
23
26
afterEach ( async ( ) => host . restore ( ) . toPromise ( ) ) ;
24
27
25
- it ( 'supports lazy bundle for lazy routes with JIT' , async ( ) => {
26
- host . writeMultipleFiles ( lazyModuleFiles ) ;
27
- host . writeMultipleFiles ( lazyModuleImport ) ;
28
-
29
- const { files } = await browserBuild ( architect , host , target ) ;
30
- expect ( 'lazy-lazy-module.js' in files ) . toBe ( true ) ;
31
- } ) ;
32
-
33
- it ( 'should show error when lazy route is invalid on watch mode AOT' , async ( ) => {
34
- host . writeMultipleFiles ( lazyModuleFiles ) ;
35
- host . writeMultipleFiles ( lazyModuleImport ) ;
36
- host . replaceInFile (
37
- 'src/app/app.module.ts' ,
38
- 'lazy.module#LazyModule' ,
39
- 'invalid.module#LazyModule' ,
40
- ) ;
41
-
42
- const logger = new TestLogger ( 'rebuild-lazy-errors' ) ;
43
- const overrides = { watch : true , aot : true } ;
44
- const run = await architect . scheduleTarget ( target , overrides , { logger } ) ;
45
- await run . output . pipe (
46
- timeout ( 15000 ) ,
47
- tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( false ) ) ,
48
- tap ( ( ) => {
49
- expect ( logger . includes ( 'Could not resolve module' ) ) . toBe ( true ) ;
50
- logger . clear ( ) ;
51
- host . appendToFile ( 'src/main.ts' , ' ' ) ;
52
- } ) ,
53
- take ( 2 ) ,
54
- ) . toPromise ( ) ;
55
- await run . stop ( ) ;
56
- } ) ;
57
-
58
- it ( 'supports lazy bundle for lazy routes with AOT' , async ( ) => {
59
- host . writeMultipleFiles ( lazyModuleFiles ) ;
60
- host . writeMultipleFiles ( lazyModuleImport ) ;
61
-
62
- const { files } = await browserBuild ( architect , host , target , { aot : true } ) ;
63
- expect ( files [ 'lazy-lazy-module-ngfactory.js' ] ) . not . toBeUndefined ( ) ;
64
- } ) ;
28
+ for ( const [ name , imports ] of Object . entries ( {
29
+ 'string' : lazyModuleStringImport ,
30
+ 'function' : lazyModuleFnImport ,
31
+ } ) ) {
32
+ describe ( `Load children ${ name } syntax` , ( ) => {
33
+ it ( 'supports lazy bundle for lazy routes with JIT' , async ( ) => {
34
+ host . writeMultipleFiles ( lazyModuleFiles ) ;
35
+ host . writeMultipleFiles ( imports ) ;
36
+
37
+ const { files } = await browserBuild ( architect , host , target ) ;
38
+ expect ( 'lazy-lazy-module.js' in files ) . toBe ( true ) ;
39
+ } ) ;
40
+
41
+ it ( 'should show error when lazy route is invalid on watch mode AOT' , async ( ) => {
42
+ host . writeMultipleFiles ( lazyModuleFiles ) ;
43
+ host . writeMultipleFiles ( imports ) ;
44
+ host . replaceInFile (
45
+ 'src/app/app.module.ts' ,
46
+ 'lazy.module' ,
47
+ 'invalid.module' ,
48
+ ) ;
49
+
50
+ const logger = new TestLogger ( 'rebuild-lazy-errors' ) ;
51
+ const overrides = { watch : true , aot : true } ;
52
+ const run = await architect . scheduleTarget ( target , overrides , { logger } ) ;
53
+ await run . output . pipe (
54
+ timeout ( 15000 ) ,
55
+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( false ) ) ,
56
+ tap ( ( ) => {
57
+ // Webpack error when using loadchildren string syntax.
58
+ const hasMissingModuleError = logger . includes ( 'Could not resolve module' )
59
+ // TS type error when using import().
60
+ || logger . includes ( 'Cannot find module' )
61
+ // Webpack error when using import() on a rebuild.
62
+ // There is no TS error because the type checker is forked on rebuilds.
63
+ || logger . includes ( 'Module not found' ) ;
64
+ expect ( hasMissingModuleError ) . toBe ( true , 'Should show missing module error' ) ;
65
+ logger . clear ( ) ;
66
+ host . appendToFile ( 'src/main.ts' , ' ' ) ;
67
+ } ) ,
68
+ take ( 2 ) ,
69
+ ) . toPromise ( ) ;
70
+ await run . stop ( ) ;
71
+ } ) ;
72
+
73
+ it ( 'supports lazy bundle for lazy routes with AOT' , async ( ) => {
74
+ host . writeMultipleFiles ( lazyModuleFiles ) ;
75
+ host . writeMultipleFiles ( imports ) ;
76
+
77
+ const { files } = await browserBuild ( architect , host , target , { aot : true } ) ;
78
+ expect ( files [ 'lazy-lazy-module-ngfactory.js' ] ) . not . toBeUndefined ( ) ;
79
+ } ) ;
80
+ } ) ;
81
+ }
65
82
66
83
it ( `supports lazy bundle for import() calls` , async ( ) => {
67
84
host . writeMultipleFiles ( {
@@ -72,7 +89,7 @@ describe('Browser Builder lazy modules', () => {
72
89
host . replaceInFile ( 'src/tsconfig.app.json' , `"module": "es2015"` , `"module": "esnext"` ) ;
73
90
74
91
const { files } = await browserBuild ( architect , host , target ) ;
75
- expect ( files [ '0 .js' ] ) . not . toBeUndefined ( ) ;
92
+ expect ( files [ 'lazy-module .js' ] ) . not . toBeUndefined ( ) ;
76
93
} ) ;
77
94
78
95
it ( `supports lazy bundle for dynamic import() calls` , async ( ) => {
@@ -96,7 +113,7 @@ describe('Browser Builder lazy modules', () => {
96
113
} ) ;
97
114
98
115
const { files } = await browserBuild ( architect , host , target ) ;
99
- expect ( files [ '0 .js' ] ) . not . toBeUndefined ( ) ;
116
+ expect ( files [ 'lazy-module .js' ] ) . not . toBeUndefined ( ) ;
100
117
} ) ;
101
118
102
119
it ( `supports hiding lazy bundle module name` , async ( ) => {
@@ -116,13 +133,12 @@ describe('Browser Builder lazy modules', () => {
116
133
'src/two.ts' : `import * as http from '@angular/common/http'; console.log(http);` ,
117
134
'src/main.ts' : `import('./one'); import('./two');` ,
118
135
} ) ;
119
- host . replaceInFile ( 'src/tsconfig.app.json' , `"module": "es2015"` , `"module": "esnext"` ) ;
120
136
121
- const { files } = await browserBuild ( architect , host , target , { namedChunks : false } ) ;
122
- expect ( files [ '0 .js' ] ) . not . toBeUndefined ( ) ;
123
- expect ( files [ '1 .js' ] ) . not . toBeUndefined ( ) ;
137
+ const { files } = await browserBuild ( architect , host , target ) ;
138
+ expect ( files [ 'one .js' ] ) . not . toBeUndefined ( ) ;
139
+ expect ( files [ 'two .js' ] ) . not . toBeUndefined ( ) ;
124
140
// TODO: the chunk with common modules used to be called `common`, see why that changed.
125
- expect ( files [ '2 .js' ] ) . not . toBeUndefined ( ) ;
141
+ expect ( files [ 'default~one~two .js' ] ) . not . toBeUndefined ( ) ;
126
142
} ) ;
127
143
128
144
it ( `supports disabling the common bundle` , async ( ) => {
@@ -131,12 +147,11 @@ describe('Browser Builder lazy modules', () => {
131
147
'src/two.ts' : `import * as http from '@angular/common/http'; console.log(http);` ,
132
148
'src/main.ts' : `import('./one'); import('./two');` ,
133
149
} ) ;
134
- host . replaceInFile ( 'src/tsconfig.app.json' , `"module": "es2015"` , `"module": "esnext"` ) ;
135
150
136
151
const { files } = await browserBuild ( architect , host , target , { commonChunk : false } ) ;
137
- expect ( files [ '0 .js' ] ) . not . toBeUndefined ( ) ;
138
- expect ( files [ '1 .js' ] ) . not . toBeUndefined ( ) ;
139
- expect ( files [ '2 .js' ] ) . toBeUndefined ( ) ;
152
+ expect ( files [ 'one .js' ] ) . not . toBeUndefined ( ) ;
153
+ expect ( files [ 'two .js' ] ) . not . toBeUndefined ( ) ;
154
+ expect ( files [ 'common .js' ] ) . toBeUndefined ( ) ;
140
155
} ) ;
141
156
142
157
it ( `supports extra lazy modules array in JIT` , async ( ) => {
0 commit comments