/
ng-add.test.ts
301 lines (274 loc) · 9.66 KB
/
ng-add.test.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
process.env.SELECTED_CLI = 'angular';
import {
checkFilesExist,
cleanupProject,
getSelectedPackageManager,
readJson,
runCLI,
runCommand,
runNgAdd,
runNgNew,
uniq,
updateFile,
} from '@nrwl/e2e/utils';
import { PackageManager } from '@nrwl/tao/src/shared/package-manager';
describe('convert Angular CLI workspace to an Nx workspace', () => {
let project: string;
let packageManager: PackageManager;
// utility to manually add protractor since it's not generated
// in the latest Angular CLI versions, but older projects updated
// to latest versions might still have it
function addProtractor() {
updateFile('e2e/protractor.conf.js', 'exports.config = {};');
updateFile(
'e2e/tsconfig.json',
JSON.stringify({ extends: '../tsconfig.json' }, null, 2)
);
updateFile(
'e2e/src/app.e2e-spec.ts',
`describe('app', () => {
it('should pass', {
expect(true).toBe(true);
});
});`
);
const angularJson = readJson('angular.json');
angularJson.projects[project].architect.e2e = {
builder: '@angular-devkit/build-angular:protractor',
options: {
protractorConfig: 'e2e/protractor.conf.js',
devServerTarget: `${project}:serve`,
},
configurations: {
production: { devServerTarget: `${project}:serve:production` },
},
};
updateFile('angular.json', JSON.stringify(angularJson, null, 2));
}
beforeEach(() => {
project = uniq('proj');
packageManager = getSelectedPackageManager();
// TODO: solve issues with pnpm and remove this fallback
packageManager = packageManager === 'pnpm' ? 'yarn' : packageManager;
runNgNew(project, packageManager);
});
afterEach(() => {
cleanupProject();
});
it('should generate a workspace', () => {
addProtractor();
// update package.json
const packageJson = readJson('package.json');
packageJson.description = 'some description';
updateFile('package.json', JSON.stringify(packageJson, null, 2));
// update tsconfig.json
const tsConfig = readJson('tsconfig.json');
tsConfig.compilerOptions.paths = { a: ['b'] };
updateFile('tsconfig.json', JSON.stringify(tsConfig, null, 2));
// add an extra script file
updateFile('src/scripts.ts', 'const x = 1;');
// update angular.json
const angularJson = readJson('angular.json');
angularJson.projects[project].architect.build.options.scripts =
angularJson.projects[project].architect.test.options.scripts = [
'src/scripts.ts',
];
angularJson.projects[project].architect.test.options.styles = [
'src/styles.css',
];
updateFile('angular.json', JSON.stringify(angularJson, null, 2));
// confirm that @nrwl dependencies do not exist yet
expect(packageJson.devDependencies['@nrwl/workspace']).not.toBeDefined();
// run ng add
runNgAdd('--npm-scope projscope');
// check that prettier config exits and that files have been moved
checkFilesExist(
'.vscode/extensions.json',
'.prettierrc',
`apps/${project}/src/main.ts`,
`apps/${project}/src/app/app.module.ts`
);
// check the right VSCode extensions are recommended
expect(readJson('.vscode/extensions.json').recommendations).toEqual([
'angular.ng-template',
'nrwl.angular-console',
'dbaeumer.vscode-eslint',
'esbenp.prettier-vscode',
]);
// check package.json
const updatedPackageJson = readJson('package.json');
expect(updatedPackageJson.description).toEqual('some description');
expect(updatedPackageJson.scripts).toEqual({
ng: 'nx',
start: 'ng serve',
build: 'ng build',
watch: 'ng build --watch --configuration development',
test: 'ng test',
nx: 'nx',
'affected:apps': 'nx affected:apps',
'affected:libs': 'nx affected:libs',
'affected:build': 'nx affected:build',
'affected:e2e': 'nx affected:e2e',
'affected:test': 'nx affected:test',
'affected:lint': 'nx affected:lint',
'affected:graph': 'nx affected:graph',
affected: 'nx affected',
format: 'nx format:write',
'format:write': 'nx format:write',
'format:check': 'nx format:check',
update: 'ng update @nrwl/workspace',
'update:check': 'ng update',
lint: 'nx workspace-lint && ng lint',
graph: 'nx graph',
'workspace-schematic': 'nx workspace-schematic',
help: 'nx help',
postinstall: 'node ./decorate-angular-cli.js',
});
expect(updatedPackageJson.devDependencies['@nrwl/workspace']).toBeDefined();
expect(updatedPackageJson.devDependencies['@angular/cli']).toBeDefined();
// check nx.json
const nxJson = readJson('nx.json');
expect(nxJson).toEqual({
npmScope: 'projscope',
affected: { defaultBase: 'main' },
implicitDependencies: {
'angular.json': '*',
'package.json': '*',
'tslint.json': '*',
'.eslintrc.json': '*',
'tsconfig.base.json': '*',
'nx.json': '*',
},
cli: { defaultCollection: '@nrwl/angular', packageManager },
defaultProject: project,
});
// check angular.json
const updatedAngularCLIJson = readJson('angular.json');
expect(updatedAngularCLIJson.projects[project].root).toEqual(
`apps/${project}`
);
expect(updatedAngularCLIJson.projects[project].sourceRoot).toEqual(
`apps/${project}/src`
);
expect(updatedAngularCLIJson.projects[project].architect.build).toEqual({
builder: '@angular-devkit/build-angular:browser',
options: {
outputPath: `dist/apps/${project}`,
index: `apps/${project}/src/index.html`,
main: `apps/${project}/src/main.ts`,
polyfills: `apps/${project}/src/polyfills.ts`,
tsConfig: `apps/${project}/tsconfig.app.json`,
assets: [
`apps/${project}/src/favicon.ico`,
`apps/${project}/src/assets`,
],
styles: [`apps/${project}/src/styles.css`],
scripts: [`apps/${project}/src/scripts.ts`],
},
configurations: {
production: {
fileReplacements: [
{
replace: `apps/${project}/src/environments/environment.ts`,
with: `apps/${project}/src/environments/environment.prod.ts`,
},
],
budgets: [
{
type: 'initial',
maximumWarning: '500kb',
maximumError: '1mb',
},
{
type: 'anyComponentStyle',
maximumWarning: '2kb',
maximumError: '4kb',
},
],
outputHashing: 'all',
},
development: {
buildOptimizer: false,
optimization: false,
vendorChunk: true,
extractLicenses: false,
sourceMap: true,
namedChunks: true,
},
},
defaultConfiguration: 'production',
});
expect(updatedAngularCLIJson.projects[project].architect.serve).toEqual({
builder: '@angular-devkit/build-angular:dev-server',
configurations: {
production: { browserTarget: `${project}:build:production` },
development: { browserTarget: `${project}:build:development` },
},
defaultConfiguration: 'development',
});
expect(updatedAngularCLIJson.projects[project].architect.test).toEqual({
builder: '@angular-devkit/build-angular:karma',
options: {
main: `apps/${project}/src/test.ts`,
polyfills: `apps/${project}/src/polyfills.ts`,
tsConfig: `apps/${project}/tsconfig.spec.json`,
karmaConfig: `apps/${project}/karma.conf.js`,
assets: [
`apps/${project}/src/favicon.ico`,
`apps/${project}/src/assets`,
],
styles: [`apps/${project}/src/styles.css`],
scripts: [`apps/${project}/src/scripts.ts`],
},
});
// check e2e project config
expect(
updatedAngularCLIJson.projects[project].architect.e2e
).toBeUndefined();
expect(updatedAngularCLIJson.projects[`${project}-e2e`].root).toEqual(
`apps/${project}-e2e`
);
expect(
updatedAngularCLIJson.projects[`${project}-e2e`].architect.e2e
).toEqual({
builder: '@angular-devkit/build-angular:protractor',
options: {
protractorConfig: `apps/${project}-e2e/protractor.conf.js`,
devServerTarget: `${project}:serve`,
},
configurations: {
production: {
devServerTarget: `${project}:serve:production`,
},
},
});
runCLI('build --configuration production --outputHashing none');
checkFilesExist(`dist/apps/${project}/main.js`);
});
it('should handle different types of errors', () => {
addProtractor();
// Remove e2e directory
runCommand('mv e2e e2e-bak');
expect(() => runNgAdd('--npm-scope projscope --skip-install')).toThrow(
'An e2e project was specified but e2e/protractor.conf.js could not be found.'
);
// Restore e2e directory
runCommand('mv e2e-bak e2e');
// TODO: this functionality is currently broken, this validation doesn't exist
// // Remove src
// runCommand('mv src src-bak');
// expect(() => runNgAdd('--npm-scope projscope --skip-install')).toThrow(
// 'Path: src does not exist'
// );
// // Put src back
// runCommand('mv src-bak src');
});
it('should support preserveAngularCliLayout', () => {
runNgAdd('--preserve-angular-cli-layout');
const updatedAngularCLIJson = readJson('angular.json');
expect(updatedAngularCLIJson.projects[project].root).toEqual('');
expect(updatedAngularCLIJson.projects[project].sourceRoot).toEqual('src');
const output = runCLI('build');
expect(output).toContain(`> nx run ${project}:build:production`);
});
});