/
library.spec.ts
346 lines (306 loc) · 10.7 KB
/
library.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import { getProjects, readJson, Tree, updateJson } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import libraryGenerator from './library';
import { Linter } from '@nrwl/linter';
import { Schema } from './schema';
describe('lib', () => {
let appTree: Tree;
const defaultSchema: Schema = {
name: 'myLib',
linter: Linter.EsLint,
skipFormat: false,
skipTsConfig: false,
unitTestRunner: 'jest',
strict: true,
};
beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
appTree.write('.gitignore', '');
});
describe('not nested', () => {
it('should update workspace.json', async () => {
await libraryGenerator(appTree, defaultSchema);
const workspaceJson = readJson(appTree, '/workspace.json');
expect(workspaceJson.projects['my-lib'].root).toEqual('libs/my-lib');
expect(workspaceJson.projects['my-lib'].architect.build).toBeUndefined();
expect(workspaceJson.projects['my-lib'].architect.lint).toEqual({
builder: '@nrwl/linter:eslint',
outputs: ['{options.outputFile}'],
options: {
lintFilePatterns: ['libs/my-lib/**/*.{ts,tsx,js,jsx}'],
},
});
});
it('should update nx.json', async () => {
await libraryGenerator(appTree, { ...defaultSchema, tags: 'one,two' });
const nxJson = readJson(appTree, '/nx.json');
expect(nxJson.projects).toEqual({
'my-lib': {
tags: ['one', 'two'],
},
});
});
it('should update tsconfig.base.json', async () => {
await libraryGenerator(appTree, defaultSchema);
const tsconfigJson = readJson(appTree, '/tsconfig.base.json');
expect(tsconfigJson.compilerOptions.paths['@proj/my-lib']).toEqual([
'libs/my-lib/src/index.ts',
]);
});
it('should update root tsconfig.base.json (no existing path mappings)', async () => {
updateJson(appTree, 'tsconfig.base.json', (json) => {
json.compilerOptions.paths = undefined;
return json;
});
await libraryGenerator(appTree, defaultSchema);
const tsconfigJson = readJson(appTree, '/tsconfig.base.json');
expect(tsconfigJson.compilerOptions.paths['@proj/my-lib']).toEqual([
'libs/my-lib/src/index.ts',
]);
});
it('should create a local tsconfig.json', async () => {
await libraryGenerator(appTree, defaultSchema);
const tsconfigJson = readJson(appTree, 'libs/my-lib/tsconfig.json');
expect(tsconfigJson.references).toEqual([
{
path: './tsconfig.lib.json',
},
{
path: './tsconfig.spec.json',
},
]);
expect(
tsconfigJson.compilerOptions.forceConsistentCasingInFileNames
).toEqual(true);
expect(tsconfigJson.compilerOptions.strict).toEqual(true);
expect(tsconfigJson.compilerOptions.noImplicitReturns).toEqual(true);
expect(tsconfigJson.compilerOptions.noFallthroughCasesInSwitch).toEqual(
true
);
});
it('should extend the local tsconfig.json with tsconfig.spec.json', async () => {
await libraryGenerator(appTree, defaultSchema);
const tsconfigJson = readJson(appTree, 'libs/my-lib/tsconfig.spec.json');
expect(tsconfigJson.extends).toEqual('./tsconfig.json');
});
it('should extend the local tsconfig.json with tsconfig.lib.json', async () => {
await libraryGenerator(appTree, defaultSchema);
const tsconfigJson = readJson(appTree, 'libs/my-lib/tsconfig.lib.json');
expect(tsconfigJson.extends).toEqual('./tsconfig.json');
});
});
describe('nested', () => {
it('should update nx.json', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
directory: 'myDir',
tags: 'one',
});
const nxJson = readJson(appTree, '/nx.json');
expect(nxJson.projects).toEqual({
'my-dir-my-lib': {
tags: ['one'],
},
});
await libraryGenerator(appTree, {
...defaultSchema,
name: 'myLib2',
directory: 'myDir',
tags: 'one,two',
});
const nxJson2 = readJson(appTree, '/nx.json');
expect(nxJson2.projects).toEqual({
'my-dir-my-lib': {
tags: ['one'],
},
'my-dir-my-lib2': {
tags: ['one', 'two'],
},
});
});
it('should update workspace.json', async () => {
await libraryGenerator(appTree, { ...defaultSchema, directory: 'myDir' });
const workspaceJson = readJson(appTree, '/workspace.json');
expect(workspaceJson.projects['my-dir-my-lib'].root).toEqual(
'libs/my-dir/my-lib'
);
expect(workspaceJson.projects['my-dir-my-lib'].architect.lint).toEqual({
builder: '@nrwl/linter:eslint',
outputs: ['{options.outputFile}'],
options: {
lintFilePatterns: ['libs/my-dir/my-lib/**/*.{ts,tsx,js,jsx}'],
},
});
});
it('should update tsconfig.base.json', async () => {
await libraryGenerator(appTree, { ...defaultSchema, directory: 'myDir' });
const tsconfigJson = readJson(appTree, '/tsconfig.base.json');
expect(tsconfigJson.compilerOptions.paths['@proj/my-dir/my-lib']).toEqual(
['libs/my-dir/my-lib/src/index.ts']
);
expect(
tsconfigJson.compilerOptions.paths['my-dir-my-lib/*']
).toBeUndefined();
});
it('should create a local tsconfig.json', async () => {
await libraryGenerator(appTree, { ...defaultSchema, directory: 'myDir' });
const tsconfigJson = readJson(
appTree,
'libs/my-dir/my-lib/tsconfig.json'
);
expect(tsconfigJson.references).toEqual([
{
path: './tsconfig.lib.json',
},
{
path: './tsconfig.spec.json',
},
]);
});
});
describe('--unit-test-runner none', () => {
it('should not generate test configuration', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
unitTestRunner: 'none',
});
expect(appTree.exists('libs/my-lib/tsconfig.spec.json')).toBeFalsy();
expect(appTree.exists('libs/my-lib/jest.config.js')).toBeFalsy();
const workspaceJson = readJson(appTree, 'workspace.json');
expect(workspaceJson.projects['my-lib'].architect.test).toBeUndefined();
expect(workspaceJson.projects['my-lib'].architect.lint)
.toMatchInlineSnapshot(`
Object {
"builder": "@nrwl/linter:eslint",
"options": Object {
"lintFilePatterns": Array [
"libs/my-lib/**/*.{ts,tsx,js,jsx}",
],
},
"outputs": Array [
"{options.outputFile}",
],
}
`);
});
});
describe('--buildable', () => {
it('should have a builder defined', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
buildable: true,
});
const workspaceJson = getProjects(appTree);
expect(workspaceJson.get('my-lib').targets.build).toBeDefined();
});
});
describe('--publishable', () => {
it('should add build architect', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
publishable: true,
importPath: '@proj/my-lib',
});
const workspaceJson = getProjects(appTree);
expect(workspaceJson.get('my-lib').targets.build).toMatchObject({
executor: '@nrwl/web:package',
outputs: ['{options.outputPath}'],
options: {
external: ['react/jsx-runtime'],
entryFile: 'libs/my-lib/src/index.ts',
outputPath: 'dist/libs/my-lib',
project: 'libs/my-lib/package.json',
tsConfig: 'libs/my-lib/tsconfig.lib.json',
rollupConfig: '@nrwl/react/plugins/bundle-rollup',
},
});
});
it('should fail if no importPath is provided with publishable', async () => {
expect.assertions(1);
try {
await libraryGenerator(appTree, {
...defaultSchema,
directory: 'myDir',
publishable: true,
});
} catch (e) {
expect(e.message).toContain(
'For publishable libs you have to provide a proper "--importPath" which needs to be a valid npm package name (e.g. my-awesome-lib or @myorg/my-lib)'
);
}
});
it('should add package.json and .babelrc', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
publishable: true,
importPath: '@proj/my-lib',
});
const packageJson = readJson(appTree, '/libs/my-lib/package.json');
expect(packageJson.name).toEqual('@proj/my-lib');
expect(appTree.exists('/libs/my-lib/.babelrc'));
});
});
describe('--js', () => {
it('should generate JS files', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
js: true,
});
expect(appTree.exists('/libs/my-lib/src/index.js')).toBe(true);
});
});
describe('--importPath', () => {
it('should update the package.json & tsconfig with the given import path', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
publishable: true,
directory: 'myDir',
importPath: '@myorg/lib',
});
const packageJson = readJson(appTree, 'libs/my-dir/my-lib/package.json');
const tsconfigJson = readJson(appTree, '/tsconfig.base.json');
expect(packageJson.name).toBe('@myorg/lib');
expect(
tsconfigJson.compilerOptions.paths[packageJson.name]
).toBeDefined();
});
it('should fail if the same importPath has already been used', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
name: 'myLib1',
publishable: true,
importPath: '@myorg/lib',
});
try {
await libraryGenerator(appTree, {
...defaultSchema,
name: 'myLib2',
publishable: true,
importPath: '@myorg/lib',
});
} catch (e) {
expect(e.message).toContain(
'You already have a library using the import path'
);
}
expect.assertions(1);
});
});
describe('--no-strict', () => {
it('should not add options for strict mode', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
strict: false,
});
const tsconfigJson = readJson(appTree, '/libs/my-lib/tsconfig.json');
expect(
tsconfigJson.compilerOptions.forceConsistentCasingInFileNames
).not.toBeDefined();
expect(tsconfigJson.compilerOptions.strict).not.toBeDefined();
expect(tsconfigJson.compilerOptions.noImplicitReturns).not.toBeDefined();
expect(
tsconfigJson.compilerOptions.noFallthroughCasesInSwitch
).not.toBeDefined();
});
});
});