99import { Architect } from '@angular-devkit/architect' ;
1010import { TestLogger } from '@angular-devkit/architect/testing' ;
1111import { 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' ;
1316
1417// tslint:disable-next-line:no-big-function
1518describe ( 'Browser Builder lazy modules' , ( ) => {
@@ -22,46 +25,60 @@ describe('Browser Builder lazy modules', () => {
2225 } ) ;
2326 afterEach ( async ( ) => host . restore ( ) . toPromise ( ) ) ;
2427
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+ }
6582
6683 it ( `supports lazy bundle for import() calls` , async ( ) => {
6784 host . writeMultipleFiles ( {
@@ -72,7 +89,7 @@ describe('Browser Builder lazy modules', () => {
7289 host . replaceInFile ( 'src/tsconfig.app.json' , `"module": "es2015"` , `"module": "esnext"` ) ;
7390
7491 const { files } = await browserBuild ( architect , host , target ) ;
75- expect ( files [ '0 .js' ] ) . not . toBeUndefined ( ) ;
92+ expect ( files [ 'lazy-module .js' ] ) . not . toBeUndefined ( ) ;
7693 } ) ;
7794
7895 it ( `supports lazy bundle for dynamic import() calls` , async ( ) => {
@@ -96,7 +113,7 @@ describe('Browser Builder lazy modules', () => {
96113 } ) ;
97114
98115 const { files } = await browserBuild ( architect , host , target ) ;
99- expect ( files [ '0 .js' ] ) . not . toBeUndefined ( ) ;
116+ expect ( files [ 'lazy-module .js' ] ) . not . toBeUndefined ( ) ;
100117 } ) ;
101118
102119 it ( `supports hiding lazy bundle module name` , async ( ) => {
@@ -116,13 +133,12 @@ describe('Browser Builder lazy modules', () => {
116133 'src/two.ts' : `import * as http from '@angular/common/http'; console.log(http);` ,
117134 'src/main.ts' : `import('./one'); import('./two');` ,
118135 } ) ;
119- host . replaceInFile ( 'src/tsconfig.app.json' , `"module": "es2015"` , `"module": "esnext"` ) ;
120136
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 ( ) ;
124140 // 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 ( ) ;
126142 } ) ;
127143
128144 it ( `supports disabling the common bundle` , async ( ) => {
@@ -131,12 +147,11 @@ describe('Browser Builder lazy modules', () => {
131147 'src/two.ts' : `import * as http from '@angular/common/http'; console.log(http);` ,
132148 'src/main.ts' : `import('./one'); import('./two');` ,
133149 } ) ;
134- host . replaceInFile ( 'src/tsconfig.app.json' , `"module": "es2015"` , `"module": "esnext"` ) ;
135150
136151 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 ( ) ;
140155 } ) ;
141156
142157 it ( `supports extra lazy modules array in JIT` , async ( ) => {
0 commit comments