/
index_spec.ts
304 lines (257 loc) · 13 KB
/
index_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
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
302
303
304
/**
* @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
*/
// tslint:disable:no-big-function
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { getFileContent } from '../../angular/utility/test';
import { Schema as ComponentOptions } from '../component/schema';
import { latestVersions } from '../utility/latest-versions';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as GenerateLibrarySchema } from './schema';
function getJsonFileContent(tree: UnitTestTree, path: string) {
return JSON.parse(tree.readContent(path));
}
// tslint:disable:max-line-length
describe('Library Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/ng_packagr',
require.resolve('../collection.json'),
);
const defaultOptions: GenerateLibrarySchema = {
name: 'foo',
entryFile: 'my-index',
skipPackageJson: false,
skipTsConfig: false,
skipInstall: false,
};
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};
let workspaceTree: UnitTestTree;
beforeEach(() => {
workspaceTree = schematicRunner.runSchematic('workspace', workspaceOptions);
});
it('should create files', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const files = tree.files;
expect(files).toEqual(jasmine.arrayContaining([
'/projects/foo/karma.conf.js',
'/projects/foo/ng-package.json',
'/projects/foo/package.json',
'/projects/foo/README.md',
'/projects/foo/tslint.json',
'/projects/foo/src/test.ts',
'/projects/foo/src/my-index.ts',
'/projects/foo/src/lib/foo.module.ts',
'/projects/foo/src/lib/foo.component.spec.ts',
'/projects/foo/src/lib/foo.component.ts',
'/projects/foo/src/lib/foo.service.spec.ts',
'/projects/foo/src/lib/foo.service.ts',
]));
});
it('should create a package.json named "foo"', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const fileContent = getFileContent(tree, '/projects/foo/package.json');
expect(fileContent).toMatch(/"name": "foo"/);
});
it('should have the latest Angular major versions in package.json named "foo"', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const fileContent = getJsonFileContent(tree, '/projects/foo/package.json');
const angularVersion = latestVersions.Angular.replace('~', '').replace('^', '');
expect(fileContent.peerDependencies['@angular/core']).toBe(`^${angularVersion}`);
});
it('should create a README.md named "foo"', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const fileContent = getFileContent(tree, '/projects/foo/README.md');
expect(fileContent).toMatch(/# Foo/);
});
it('should create a tsconfig for library', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const fileContent = getJsonFileContent(tree, '/projects/foo/tsconfig.lib.json');
expect(fileContent).toBeDefined();
});
it('should create a ng-package.json with ngPackage conf', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const fileContent = getJsonFileContent(tree, '/projects/foo/ng-package.json');
expect(fileContent.lib).toBeDefined();
expect(fileContent.lib.entryFile).toEqual('src/my-index.ts');
expect(fileContent.dest).toEqual('../../dist/foo');
});
it('should use default value for baseDir and entryFile', () => {
const tree = schematicRunner.runSchematic('library', {
name: 'foobar',
}, workspaceTree);
expect(tree.files).toContain('/projects/foobar/src/public-api.ts');
});
it(`should add library to workspace`, () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const workspace = getJsonFileContent(tree, '/angular.json');
expect(workspace.projects.foo).toBeDefined();
expect(workspace.defaultProject).toBe('foo');
});
it('should set the prefix to lib if none is set', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo.prefix).toEqual('lib');
});
it('should set the prefix correctly', () => {
const options = { ...defaultOptions, prefix: 'pre' };
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo.prefix).toEqual('pre');
});
it('should set the right prefix in the tslint file when provided is kebabed', () => {
const options: GenerateLibrarySchema = { ...defaultOptions, prefix: 'foo-bar' };
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
const path = '/projects/foo/tslint.json';
const content = JSON.parse(tree.readContent(path));
expect(content.rules['directive-selector'][2]).toMatch('fooBar');
expect(content.rules['component-selector'][2]).toMatch('foo-bar');
});
it('should handle a pascalCasedName', () => {
const options = {...defaultOptions, name: 'pascalCasedName'};
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
const config = getJsonFileContent(tree, '/angular.json');
const project = config.projects.pascalCasedName;
expect(project).toBeDefined();
expect(project.root).toEqual('projects/pascal-cased-name');
const svcContent = tree.readContent('/projects/pascal-cased-name/src/lib/pascal-cased-name.service.ts');
expect(svcContent).toMatch(/providedIn: 'root'/);
});
it('should export the component in the NgModule', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const fileContent = getFileContent(tree, '/projects/foo/src/lib/foo.module.ts');
expect(fileContent).toContain('exports: [FooComponent]');
});
it('should set the right path and prefix in the tslint file', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const path = '/projects/foo/tslint.json';
const content = JSON.parse(tree.readContent(path));
expect(content.extends).toMatch('../../tslint.json');
expect(content.rules['directive-selector'][2]).toMatch('lib');
expect(content.rules['component-selector'][2]).toMatch('lib');
});
describe(`update package.json`, () => {
it(`should add ng-packagr to devDependencies`, () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const packageJson = getJsonFileContent(tree, 'package.json');
expect(packageJson.devDependencies['ng-packagr']).toEqual('^4.2.0');
expect(packageJson.devDependencies['@angular-devkit/build-ng-packagr'])
.toEqual(latestVersions.DevkitBuildNgPackagr);
});
it('should use the latest known versions in package.json', () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const pkg = JSON.parse(tree.readContent('/package.json'));
expect(pkg.devDependencies['@angular/compiler-cli']).toEqual(latestVersions.Angular);
expect(pkg.devDependencies['typescript']).toEqual(latestVersions.TypeScript);
});
it(`should not override existing users dependencies`, () => {
const oldPackageJson = workspaceTree.readContent('package.json');
workspaceTree.overwrite('package.json', oldPackageJson.replace(
`"typescript": "${latestVersions.TypeScript}"`,
`"typescript": "~2.5.2"`,
));
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const packageJson = getJsonFileContent(tree, 'package.json');
expect(packageJson.devDependencies.typescript).toEqual('~2.5.2');
});
it(`should not modify the file when --skipPackageJson`, () => {
const tree = schematicRunner.runSchematic('library', {
name: 'foo',
skipPackageJson: true,
}, workspaceTree);
const packageJson = getJsonFileContent(tree, 'package.json');
expect(packageJson.devDependencies['ng-packagr']).toBeUndefined();
expect(packageJson.devDependencies['@angular-devkit/build-angular']).toBeUndefined();
});
});
describe(`update tsconfig.json`, () => {
it(`should add paths mapping to empty tsconfig`, () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json');
expect(tsConfigJson.compilerOptions.paths.foo).toBeTruthy();
expect(tsConfigJson.compilerOptions.paths.foo.length).toEqual(1);
expect(tsConfigJson.compilerOptions.paths.foo[0]).toEqual('dist/foo');
expect(tsConfigJson.compilerOptions.paths['foo/*']).toBeTruthy();
expect(tsConfigJson.compilerOptions.paths['foo/*'].length).toEqual(1);
expect(tsConfigJson.compilerOptions.paths['foo/*'][0]).toEqual('dist/foo/*');
});
it(`should append to existing paths mappings`, () => {
workspaceTree.overwrite('tsconfig.json', JSON.stringify({
compilerOptions: {
paths: {
'unrelated': ['./something/else.ts'],
'foo': ['libs/*'],
},
},
}));
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json');
expect(tsConfigJson.compilerOptions.paths.foo).toBeTruthy();
expect(tsConfigJson.compilerOptions.paths.foo.length).toEqual(2);
expect(tsConfigJson.compilerOptions.paths.foo[1]).toEqual('dist/foo');
});
it(`should not modify the file when --skipTsConfig`, () => {
const tree = schematicRunner.runSchematic('library', {
name: 'foo',
skipTsConfig: true,
}, workspaceTree);
const tsConfigJson = getJsonFileContent(tree, 'tsconfig.json');
expect(tsConfigJson.compilerOptions.paths).toBeUndefined();
});
});
it('should generate inside of a library', () => {
let tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const componentOptions: ComponentOptions = {
name: 'comp',
project: 'foo',
};
tree = schematicRunner.runSchematic('component', componentOptions, tree);
expect(tree.exists('/projects/foo/src/lib/comp/comp.component.ts')).toBe(true);
});
it(`should support creating scoped libraries`, () => {
const scopedName = '@myscope/mylib';
const options = { ...defaultOptions, name: scopedName };
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
const pkgJsonPath = '/projects/myscope/mylib/package.json';
expect(tree.files).toContain(pkgJsonPath);
expect(tree.files).toContain('/projects/myscope/mylib/src/lib/mylib.module.ts');
expect(tree.files).toContain('/projects/myscope/mylib/src/lib/mylib.component.ts');
const pkgJson = JSON.parse(tree.readContent(pkgJsonPath));
expect(pkgJson.name).toEqual(scopedName);
const tsConfigJson = JSON.parse(tree.readContent('/projects/myscope/mylib/tsconfig.spec.json'));
expect(tsConfigJson.extends).toEqual('../../../tsconfig.json');
const cfg = JSON.parse(tree.readContent('/angular.json'));
expect(cfg.projects['@myscope/mylib']).toBeDefined();
const rootTsCfg = JSON.parse(tree.readContent('/tsconfig.json'));
expect(rootTsCfg.compilerOptions.paths['@myscope/mylib']).toEqual(['dist/myscope/mylib']);
const karmaConf = getFileContent(tree, '/projects/myscope/mylib/karma.conf.js');
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../../coverage/myscope/mylib')`);
});
it(`should dasherize scoped libraries`, () => {
const scopedName = '@myScope/myLib';
const expectedScopeName = '@my-scope/my-lib';
const expectedFolderName = 'my-scope/my-lib';
const options = { ...defaultOptions, name: scopedName };
const tree = schematicRunner.runSchematic('library', options, workspaceTree);
const pkgJsonPath = '/projects/my-scope/my-lib/package.json';
expect(tree.readContent(pkgJsonPath)).toContain(expectedScopeName);
const ngPkgJsonPath = '/projects/my-scope/my-lib/ng-package.json';
expect(tree.readContent(ngPkgJsonPath)).toContain(expectedFolderName);
const pkgJson = JSON.parse(tree.readContent(pkgJsonPath));
expect(pkgJson.name).toEqual(expectedScopeName);
const cfg = JSON.parse(tree.readContent('/angular.json'));
expect(cfg.projects['@myScope/myLib']).toBeDefined();
});
it(`should set coverage folder to "coverage/foo"`, () => {
const tree = schematicRunner.runSchematic('library', defaultOptions, workspaceTree);
const karmaConf = getFileContent(tree, '/projects/foo/karma.conf.js');
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../coverage/foo')`);
});
});