-
Notifications
You must be signed in to change notification settings - Fork 12k
/
update-app-tsconfigs_spec.ts
253 lines (219 loc) · 9.63 KB
/
update-app-tsconfigs_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { EmptyTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { getWorkspaceTargets, updateWorkspaceTargets } from './update-workspace-config_spec';
// tslint:disable-next-line: no-any
function overrideJsonFile(tree: UnitTestTree, path: string, newContent: object) {
tree.overwrite(path, JSON.stringify(newContent, undefined, 2));
}
const defaultTsConfigOptions = {
extends: './tsconfig.json',
compilerOptions: {
module: 'es2015',
outDir: './out-tsc/app',
types: [],
},
exclude: [
'src/test.ts',
'src/**/*.spec.ts',
],
angularCompilerOptions: {
enableIvy: true,
},
};
// tslint:disable:no-big-function
describe('Migration to version 9', () => {
describe('Update applications tsconfig', () => {
const schematicRunner = new SchematicTestRunner(
'migrations',
require.resolve('../migration-collection.json'),
);
let tree: UnitTestTree;
beforeEach(async () => {
tree = new UnitTestTree(new EmptyTree());
tree = await schematicRunner
.runExternalSchematicAsync(
require.resolve('../../collection.json'),
'ng-new',
{
name: 'migration-test',
version: '1.2.3',
directory: '.',
},
tree,
)
.toPromise();
});
it('should update apps tsConfig with stricter files inclusions', async () => {
overrideJsonFile(tree, 'tsconfig.app.json', defaultTsConfigOptions);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { exclude, files } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(exclude).toBeUndefined();
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
});
it('should resolve paths correctly even if they are using windows separators', async () => {
const tree2 = await schematicRunner
.runExternalSchematicAsync(
require.resolve('../../collection.json'),
'application',
{
name: 'another-app',
},
tree,
)
.toPromise();
const tsCfgPath = 'projects/another-app/tsconfig.app.json';
overrideJsonFile(tree2, tsCfgPath, defaultTsConfigOptions);
const config = getWorkspaceTargets(tree2, 'another-app');
config.build.options.main = 'projects\\another-app\\src\\main.ts';
config.build.options.polyfills = 'projects\\another-app\\src\\polyfills.ts';
config.build.options.tsConfig = 'projects\\another-app\\tsconfig.app.json';
updateWorkspaceTargets(tree2, config, 'another-app');
const tree3 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree2.branch()).toPromise();
const { exclude, files } = JSON.parse(tree3.readContent(tsCfgPath));
expect(exclude).toBeUndefined();
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
});
it('should update apps tsConfig when tsconfig has include', async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
include: ['foo.ts'],
};
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { files, include } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(include).toEqual(['foo.ts']);
});
it(`should update include '**/*.ts' in apps tsConfig to '**/*.d.ts'`, async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
include: ['src/**/*.ts'],
exclude: ['test.ts'],
};
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { files, include, exclude } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(include).toEqual(['src/**/*.d.ts']);
expect(exclude).toBeUndefined();
});
it(`should update include '**/*.ts' in apps tsConfig to '**/*.d.ts' when includes contains multiple elements`, async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
include: ['foo.ts', 'src/**/*.ts'],
};
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { files, include, exclude } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(include).toEqual(['foo.ts', 'src/**/*.d.ts']);
expect(exclude).toBeUndefined();
});
it(`should remove angularCompilerOptions when enableIvy is true and it's the only option`, async () => {
overrideJsonFile(tree, 'tsconfig.app.json', defaultTsConfigOptions);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { angularCompilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(angularCompilerOptions).toBeUndefined();
});
it('should remove enableIvy only when true and there are other angularCompilerOptions', async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
angularCompilerOptions: {
enableIvy: true,
fullTemplateTypeCheck: true,
},
};
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { angularCompilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(angularCompilerOptions.enableIvy).toBeUndefined();
expect(angularCompilerOptions.fullTemplateTypeCheck).toBe(true);
});
it('should note remove enableIvy is set to false', async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
angularCompilerOptions: {
enableIvy: false,
fullTemplateTypeCheck: true,
},
};
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { angularCompilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(angularCompilerOptions.enableIvy).toBe(false);
expect(angularCompilerOptions.fullTemplateTypeCheck).toBe(true);
});
it(`should remove 'module' if in an extended configuration`, async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
angularCompilerOptions: {
enableIvy: true,
fullTemplateTypeCheck: true,
},
};
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { compilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(compilerOptions.module).toBeUndefined();
const { compilerOptions: workspaceCompilerOptions } = JSON.parse(tree2.readContent('tsconfig.json'));
expect(workspaceCompilerOptions.module).toBe('esnext');
});
it(`should set 'module' to 'esnext' if app tsconfig is not extended`, async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
extends: undefined,
angularCompilerOptions: {
enableIvy: true,
fullTemplateTypeCheck: true,
},
};
overrideJsonFile(tree, 'tsconfig.app.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { compilerOptions } = JSON.parse(tree2.readContent('tsconfig.app.json'));
expect(compilerOptions.module).toBe('esnext');
});
it(`should set 'module' to 'commonjs' in server tsconfig`, async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
compilerOptions: {
module: 'es2015',
outDir: './out-tsc/server',
},
angularCompilerOptions: {
enableIvy: true,
},
};
tree = await schematicRunner
.runExternalSchematicAsync(
require.resolve('../../collection.json'),
'universal',
{
clientProject: 'migration-test',
},
tree,
)
.toPromise();
overrideJsonFile(tree, 'tsconfig.server.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { compilerOptions } = JSON.parse(tree2.readContent('tsconfig.server.json'));
expect(compilerOptions.module).toBe('commonjs');
});
it(`should set 'module' to 'esnext' in workspace tsconfig`, async () => {
const tsConfigContent = {
...defaultTsConfigOptions,
extends: undefined,
};
overrideJsonFile(tree, 'tsconfig.json', tsConfigContent);
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
const { compilerOptions } = JSON.parse(tree2.readContent('tsconfig.json'));
expect(compilerOptions.module).toBe('esnext');
});
});
});