6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
+ import { JsonParseMode , JsonValue , parseJson } from '@angular-devkit/core' ;
9
10
import { EmptyTree } from '@angular-devkit/schematics' ;
10
11
import { SchematicTestRunner , UnitTestTree } from '@angular-devkit/schematics/testing' ;
11
12
import { getWorkspaceTargets , updateWorkspaceTargets } from './update-workspace-config_spec' ;
12
13
13
14
// 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
+
14
19
function overrideJsonFile ( tree : UnitTestTree , path : string , newContent : object ) {
15
20
tree . overwrite ( path , JSON . stringify ( newContent , undefined , 2 ) ) ;
16
21
}
@@ -63,7 +68,7 @@ describe('Migration to version 9', () => {
63
68
it ( 'should update apps tsConfig with stricter files inclusions' , async ( ) => {
64
69
overrideJsonFile ( tree , 'tsconfig.app.json' , defaultTsConfigOptions ) ;
65
70
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' ) ;
67
72
expect ( exclude ) . toBeUndefined ( ) ;
68
73
expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
69
74
expect ( include ) . toEqual ( [ 'src/**/*.d.ts' ] ) ;
@@ -90,7 +95,7 @@ describe('Migration to version 9', () => {
90
95
updateWorkspaceTargets ( tree2 , config , 'another-app' ) ;
91
96
92
97
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 ) ;
94
99
expect ( exclude ) . toBeUndefined ( ) ;
95
100
expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
96
101
} ) ;
@@ -104,7 +109,7 @@ describe('Migration to version 9', () => {
104
109
overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
105
110
106
111
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' ) ;
108
113
expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
109
114
expect ( include ) . toEqual ( [ 'foo.ts' ] ) ;
110
115
} ) ;
@@ -119,7 +124,7 @@ describe('Migration to version 9', () => {
119
124
overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
120
125
121
126
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' ) ;
123
128
expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
124
129
expect ( include ) . toEqual ( [ 'src/**/*.d.ts' ] ) ;
125
130
expect ( exclude ) . toBeUndefined ( ) ;
@@ -134,7 +139,7 @@ describe('Migration to version 9', () => {
134
139
overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
135
140
136
141
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' ) ;
138
143
expect ( files ) . toEqual ( [ 'src/main.ts' , 'src/polyfills.ts' ] ) ;
139
144
expect ( include ) . toEqual ( [ 'foo.ts' , 'src/**/*.d.ts' ] ) ;
140
145
expect ( exclude ) . toBeUndefined ( ) ;
@@ -143,7 +148,7 @@ describe('Migration to version 9', () => {
143
148
it ( `should remove angularCompilerOptions when enableIvy is true and it's the only option` , async ( ) => {
144
149
overrideJsonFile ( tree , 'tsconfig.app.json' , defaultTsConfigOptions ) ;
145
150
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' ) ;
147
152
expect ( angularCompilerOptions ) . toBeUndefined ( ) ;
148
153
} ) ;
149
154
@@ -158,7 +163,7 @@ describe('Migration to version 9', () => {
158
163
159
164
overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
160
165
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' ) ;
162
167
expect ( angularCompilerOptions . enableIvy ) . toBeUndefined ( ) ;
163
168
expect ( angularCompilerOptions . fullTemplateTypeCheck ) . toBe ( true ) ;
164
169
} ) ;
@@ -174,7 +179,7 @@ describe('Migration to version 9', () => {
174
179
175
180
overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
176
181
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' ) ;
178
183
expect ( angularCompilerOptions . enableIvy ) . toBe ( false ) ;
179
184
expect ( angularCompilerOptions . fullTemplateTypeCheck ) . toBe ( true ) ;
180
185
} ) ;
@@ -190,10 +195,10 @@ describe('Migration to version 9', () => {
190
195
191
196
overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
192
197
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' ) ;
194
199
expect ( compilerOptions . module ) . toBeUndefined ( ) ;
195
200
196
- const { compilerOptions : workspaceCompilerOptions } = JSON . parse ( tree2 . readContent ( 'tsconfig.json' ) ) ;
201
+ const { compilerOptions : workspaceCompilerOptions } = readJsonFile ( tree2 , 'tsconfig.json' ) ;
197
202
expect ( workspaceCompilerOptions . module ) . toBe ( 'esnext' ) ;
198
203
} ) ;
199
204
@@ -209,7 +214,7 @@ describe('Migration to version 9', () => {
209
214
210
215
overrideJsonFile ( tree , 'tsconfig.app.json' , tsConfigContent ) ;
211
216
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' ) ;
213
218
expect ( compilerOptions . module ) . toBe ( 'esnext' ) ;
214
219
} ) ;
215
220
@@ -238,7 +243,7 @@ describe('Migration to version 9', () => {
238
243
239
244
overrideJsonFile ( tree , 'tsconfig.server.json' , tsConfigContent ) ;
240
245
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' ) ;
242
247
expect ( compilerOptions . module ) . toBe ( 'commonjs' ) ;
243
248
} ) ;
244
249
@@ -250,7 +255,7 @@ describe('Migration to version 9', () => {
250
255
251
256
overrideJsonFile ( tree , 'tsconfig.json' , tsConfigContent ) ;
252
257
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' ) ;
254
259
expect ( compilerOptions . module ) . toBe ( 'esnext' ) ;
255
260
} ) ;
256
261
} ) ;
0 commit comments