66 * found in the LICENSE file at https://angular.io/license
77 */
88
9+ import { JsonParseMode , JsonValue , parseJson } from '@angular-devkit/core' ;
910import { EmptyTree } from '@angular-devkit/schematics' ;
1011import { SchematicTestRunner , UnitTestTree } from '@angular-devkit/schematics/testing' ;
1112import { getWorkspaceTargets , updateWorkspaceTargets } from './update-workspace-config_spec' ;
1213
1314// tslint:disable-next-line: no-any
15+ function readJsonFile ( tree : UnitTestTree , path : string ) : any {
16+ return parseJson ( tree . readContent ( path ) . toString ( ) , JsonParseMode . Loose ) ;
17+ }
18+
1419function overrideJsonFile ( tree : UnitTestTree , path : string , newContent : object ) {
1520 tree . overwrite ( path , JSON . stringify ( newContent , undefined , 2 ) ) ;
1621}
@@ -63,7 +68,7 @@ describe('Migration to version 9', () => {
6368 it ( 'should update apps tsConfig with stricter files inclusions' , async ( ) => {
6469 overrideJsonFile ( tree , 'tsconfig.app.json' , defaultTsConfigOptions ) ;
6570 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
66- const { exclude, files, include } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
71+ const { exclude, files, include } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
6772 expect ( exclude ) . toBeUndefined ( ) ;
6873 expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
6974 expect ( include ) . toEqual ( [ 'src/**/*.d.ts' ] ) ;
@@ -90,7 +95,7 @@ describe('Migration to version 9', () => {
9095 updateWorkspaceTargets ( tree2 , config , 'another-app' ) ;
9196
9297 const tree3 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree2 . branch ( ) ) . toPromise ( ) ;
93- const { exclude, files } = JSON . parse ( tree3 . readContent ( tsCfgPath ) ) ;
98+ const { exclude, files } = readJsonFile ( tree3 , tsCfgPath ) ;
9499 expect ( exclude ) . toBeUndefined ( ) ;
95100 expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
96101 } ) ;
@@ -104,7 +109,7 @@ describe('Migration to version 9', () => {
104109 overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
105110
106111 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
107- const { files, include } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
112+ const { files, include } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
108113 expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
109114 expect ( include ) . toEqual ( [ 'foo.ts' ] ) ;
110115 } ) ;
@@ -119,7 +124,7 @@ describe('Migration to version 9', () => {
119124 overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
120125
121126 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
122- const { files, include, exclude } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
127+ const { files, include, exclude } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
123128 expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
124129 expect ( include ) . toEqual ( [ 'src/**/*.d.ts' ] ) ;
125130 expect ( exclude ) . toBeUndefined ( ) ;
@@ -134,7 +139,7 @@ describe('Migration to version 9', () => {
134139 overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
135140
136141 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
137- const { files, include, exclude } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
142+ const { files, include, exclude } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
138143 expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
139144 expect ( include ) . toEqual ( [ 'foo.ts' , 'src/**/*.d.ts' ] ) ;
140145 expect ( exclude ) . toBeUndefined ( ) ;
@@ -143,7 +148,7 @@ describe('Migration to version 9', () => {
143148 it ( `should remove angularCompilerOptions when enableIvy is true and it's the only option` , async ( ) => {
144149 overrideJsonFile ( tree , 'tsconfig.app.json' , defaultTsConfigOptions ) ;
145150 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
146- const { angularCompilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
151+ const { angularCompilerOptions } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
147152 expect ( angularCompilerOptions ) . toBeUndefined ( ) ;
148153 } ) ;
149154
@@ -158,7 +163,7 @@ describe('Migration to version 9', () => {
158163
159164 overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
160165 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
161- const { angularCompilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
166+ const { angularCompilerOptions } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
162167 expect ( angularCompilerOptions . enableIvy ) . toBeUndefined ( ) ;
163168 expect ( angularCompilerOptions . fullTemplateTypeCheck ) . toBe ( true ) ;
164169 } ) ;
@@ -174,7 +179,7 @@ describe('Migration to version 9', () => {
174179
175180 overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
176181 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
177- const { angularCompilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
182+ const { angularCompilerOptions } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
178183 expect ( angularCompilerOptions . enableIvy ) . toBe ( false ) ;
179184 expect ( angularCompilerOptions . fullTemplateTypeCheck ) . toBe ( true ) ;
180185 } ) ;
@@ -190,10 +195,10 @@ describe('Migration to version 9', () => {
190195
191196 overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
192197 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
193- const { compilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
198+ const { compilerOptions } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
194199 expect ( compilerOptions . module ) . toBeUndefined ( ) ;
195200
196- const { compilerOptions : workspaceCompilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.json' ) ) ;
201+ const { compilerOptions : workspaceCompilerOptions } = readJsonFile ( tree2 , 'tsconfig.json' ) ;
197202 expect ( workspaceCompilerOptions . module ) . toBe ( 'esnext' ) ;
198203 } ) ;
199204
@@ -209,7 +214,7 @@ describe('Migration to version 9', () => {
209214
210215 overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
211216 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
212- const { compilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.app.json' ) ) ;
217+ const { compilerOptions } = readJsonFile ( tree2 , 'tsconfig.app.json' ) ;
213218 expect ( compilerOptions . module ) . toBe ( 'esnext' ) ;
214219 } ) ;
215220
@@ -238,7 +243,7 @@ describe('Migration to version 9', () => {
238243
239244 overrideJsonFile ( tree , 'tsconfig.server.json' , tsConfigContent ) ;
240245 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
241- const { compilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.server.json' ) ) ;
246+ const { compilerOptions } = readJsonFile ( tree2 , 'tsconfig.server.json' ) ;
242247 expect ( compilerOptions . module ) . toBe ( 'commonjs' ) ;
243248 } ) ;
244249
@@ -250,7 +255,7 @@ describe('Migration to version 9', () => {
250255
251256 overrideJsonFile ( tree , 'tsconfig.json' , tsConfigContent ) ;
252257 const tree2 = await schematicRunner . runSchematicAsync ( 'workspace-version-9' , { } , tree . branch ( ) ) . toPromise ( ) ;
253- const { compilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.json' ) ) ;
258+ const { compilerOptions } = readJsonFile ( tree2 , 'tsconfig.json' ) ;
254259 expect ( compilerOptions . module ) . toBe ( 'esnext' ) ;
255260 } ) ;
256261 } ) ;
0 commit comments