@@ -113,6 +113,78 @@ describe(`Migration to use the application builder`, () => {
113113 } ) ;
114114 } ) ;
115115
116+ it ( `should replace 'serviceWorker: true' with the default 'ngsw-config.json' path` , async ( ) => {
117+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
118+ const workspaceContent = tree . readJson ( '/angular.json' ) as Record < string , any > ;
119+ workspaceContent [ 'projects' ] [ 'app' ] [ 'architect' ] [ 'build' ] [ 'options' ] [ 'serviceWorker' ] = true ;
120+ tree . overwrite ( '/angular.json' , JSON . stringify ( workspaceContent ) ) ;
121+
122+ const newTree = await schematicRunner . runSchematic ( schematicName , { } , tree ) ;
123+ const {
124+ projects : { app } ,
125+ } = JSON . parse ( newTree . readContent ( '/angular.json' ) ) ;
126+
127+ const { serviceWorker, ngswConfigPath } = app . architect [ 'build' ] . options ;
128+ expect ( serviceWorker ) . toBe ( '/project/app/ngsw-config.json' ) ;
129+ expect ( ngswConfigPath ) . toBeUndefined ( ) ;
130+ } ) ;
131+
132+ it ( `should replace 'serviceWorker: true' in a configuration with the base 'ngswConfigPath'` , async ( ) => {
133+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
134+ const workspaceContent = tree . readJson ( '/angular.json' ) as Record < string , any > ;
135+ const build = workspaceContent [ 'projects' ] [ 'app' ] [ 'architect' ] [ 'build' ] ;
136+ build [ 'options' ] [ 'ngswConfigPath' ] = 'src/ngsw-config.json' ;
137+ build [ 'configurations' ] = { production : { serviceWorker : true } } ;
138+ tree . overwrite ( '/angular.json' , JSON . stringify ( workspaceContent ) ) ;
139+
140+ const newTree = await schematicRunner . runSchematic ( schematicName , { } , tree ) ;
141+ const {
142+ projects : { app } ,
143+ } = JSON . parse ( newTree . readContent ( '/angular.json' ) ) ;
144+
145+ expect ( app . architect [ 'build' ] . configurations . production . serviceWorker ) . toBe (
146+ 'src/ngsw-config.json' ,
147+ ) ;
148+ expect ( app . architect [ 'build' ] . options . serviceWorker ) . toBeUndefined ( ) ;
149+ } ) ;
150+
151+ it ( `should not enable the service worker when 'serviceWorker' is false` , async ( ) => {
152+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
153+ const workspaceContent = tree . readJson ( '/angular.json' ) as Record < string , any > ;
154+ const build = workspaceContent [ 'projects' ] [ 'app' ] [ 'architect' ] [ 'build' ] ;
155+ build [ 'options' ] [ 'serviceWorker' ] = false ;
156+ build [ 'options' ] [ 'ngswConfigPath' ] = 'src/ngsw-config.json' ;
157+ tree . overwrite ( '/angular.json' , JSON . stringify ( workspaceContent ) ) ;
158+
159+ const newTree = await schematicRunner . runSchematic ( schematicName , { } , tree ) ;
160+ const {
161+ projects : { app } ,
162+ } = JSON . parse ( newTree . readContent ( '/angular.json' ) ) ;
163+
164+ const { serviceWorker, ngswConfigPath } = app . architect [ 'build' ] . options ;
165+ expect ( serviceWorker ) . toBeFalse ( ) ;
166+ expect ( ngswConfigPath ) . toBeUndefined ( ) ;
167+ } ) ;
168+
169+ it ( `should use a configuration 'ngswConfigPath' when the base options enable the service worker` , async ( ) => {
170+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
171+ const workspaceContent = tree . readJson ( '/angular.json' ) as Record < string , any > ;
172+ const build = workspaceContent [ 'projects' ] [ 'app' ] [ 'architect' ] [ 'build' ] ;
173+ build [ 'options' ] [ 'serviceWorker' ] = true ;
174+ build [ 'configurations' ] = { production : { ngswConfigPath : 'src/ngsw-config.prod.json' } } ;
175+ tree . overwrite ( '/angular.json' , JSON . stringify ( workspaceContent ) ) ;
176+
177+ const newTree = await schematicRunner . runSchematic ( schematicName , { } , tree ) ;
178+ const {
179+ projects : { app } ,
180+ } = JSON . parse ( newTree . readContent ( '/angular.json' ) ) ;
181+
182+ expect ( app . architect [ 'build' ] . options . serviceWorker ) . toBe ( '/project/app/ngsw-config.json' ) ;
183+ expect ( app . architect [ 'build' ] . configurations . production . serviceWorker ) . toBe (
184+ 'src/ngsw-config.prod.json' ,
185+ ) ;
186+ } ) ;
187+
116188 it ( `should remove 'builderMode' from karma options` , async ( ) => {
117189 addWorkspaceTarget ( tree , 'test' , {
118190 'builder' : Builders . Karma ,
0 commit comments