forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_spec.ts
118 lines (105 loc) · 3.93 KB
/
migration_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
/**
* @license
* Copyright Google LLC 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.dev/license
*/
import { isJsonObject } from '@angular-devkit/core';
import { EmptyTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { Builders, ProjectType, WorkspaceSchema } from '../../utility/workspace-models';
describe('Migration to update moduleResolution', () => {
const schematicName = 'update-module-resolution';
const schematicRunner = new SchematicTestRunner(
'migrations',
require.resolve('../migration-collection.json'),
);
function createJsonFile(tree: UnitTestTree, filePath: string, content: {}): void {
const stringifiedContent = JSON.stringify(content, undefined, 2);
if (tree.exists(filePath)) {
tree.overwrite(filePath, stringifiedContent);
} else {
tree.create(filePath, stringifiedContent);
}
}
function getCompilerOptionsValue(tree: UnitTestTree, filePath: string): Record<string, unknown> {
const json = tree.readJson(filePath);
if (isJsonObject(json) && isJsonObject(json.compilerOptions)) {
return json.compilerOptions;
}
throw new Error(`Cannot retrieve 'compilerOptions'.`);
}
const angularConfig: WorkspaceSchema = {
version: 1,
projects: {
app: {
root: '',
sourceRoot: 'src',
projectType: ProjectType.Application,
prefix: 'app',
architect: {
build: {
builder: Builders.Browser,
options: {
tsConfig: 'src/tsconfig.app.json',
main: '',
polyfills: '',
},
configurations: {
production: {
tsConfig: 'src/tsconfig.app.prod.json',
},
},
},
test: {
builder: Builders.Karma,
options: {
karmaConfig: '',
tsConfig: 'src/tsconfig.spec.json',
},
},
},
},
},
};
let tree: UnitTestTree;
beforeEach(() => {
tree = new UnitTestTree(new EmptyTree());
const compilerOptions = { module: 'es2020', moduleResolution: 'node' };
const configWithExtends = { extends: './tsconfig.json', compilerOptions };
// Workspace
createJsonFile(tree, 'angular.json', angularConfig);
createJsonFile(tree, 'tsconfig.json', { compilerOptions });
// Application
createJsonFile(tree, 'src/tsconfig.app.json', configWithExtends);
createJsonFile(tree, 'src/tsconfig.app.prod.json', configWithExtends);
createJsonFile(tree, 'src/tsconfig.spec.json', { compilerOptions });
});
it(`should update moduleResolution to 'bundler' in workspace 'tsconfig.json'`, async () => {
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const compilerOptions = getCompilerOptionsValue(newTree, 'tsconfig.json');
expect(compilerOptions).toEqual(
jasmine.objectContaining({
moduleResolution: 'bundler',
}),
);
});
it(`should update moduleResolution to 'bundler' in builder tsconfig`, async () => {
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const compilerOptions = getCompilerOptionsValue(newTree, 'src/tsconfig.spec.json');
expect(compilerOptions).toEqual(
jasmine.objectContaining({
moduleResolution: 'bundler',
}),
);
});
it('should not update moduleResolution when module is preserve', async () => {
createJsonFile(tree, 'tsconfig.json', {
compilerOptions: { module: 'preserve', moduleResolution: 'node' },
});
const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);
const compilerOptions = getCompilerOptionsValue(newTree, 'tsconfig.json');
expect(compilerOptions).toEqual({ module: 'preserve', moduleResolution: 'node' });
});
});