-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpackages.unit.test.ts
More file actions
377 lines (347 loc) · 11.1 KB
/
packages.unit.test.ts
File metadata and controls
377 lines (347 loc) · 11.1 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { vol } from 'memfs';
import path from 'node:path';
import type { PackageJson } from 'type-fest';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import {
hasCodePushUpDependency,
hasDependency,
hasScript,
hasWorkspacesEnabled,
listPackages,
listWorkspaces,
readRootPackageJson,
} from './packages.js';
const pkgJsonContent = (content: PackageJson) =>
JSON.stringify(content, null, 2);
describe('listPackages', () => {
it('should search for all npm packages recursively by default', async () => {
vol.fromJSON(
{
'e2e/package.json': pkgJsonContent({ name: 'e2e' }),
'package.json': pkgJsonContent({ name: 'example-monorepo' }),
'packages/cli/package.json': pkgJsonContent({ name: '@example/cli' }),
'packages/core/package.json': pkgJsonContent({ name: '@example/core' }),
},
MEMFS_VOLUME,
);
await expect(listPackages(MEMFS_VOLUME)).resolves.toEqual([
{
name: 'e2e',
directory: path.join(MEMFS_VOLUME, 'e2e'),
packageJson: { name: 'e2e' },
},
{
name: 'example-monorepo',
directory: path.join(MEMFS_VOLUME),
packageJson: { name: 'example-monorepo' },
},
{
name: '@example/cli',
directory: path.join(MEMFS_VOLUME, 'packages', 'cli'),
packageJson: { name: '@example/cli' },
},
{
name: '@example/core',
directory: path.join(MEMFS_VOLUME, 'packages', 'core'),
packageJson: { name: '@example/core' },
},
]);
});
it('should search for package.json files with custom glob patterns', async () => {
vol.fromJSON(
{
'e2e/package.json': pkgJsonContent({ name: 'e2e' }),
'package.json': pkgJsonContent({ name: 'example-monorepo' }), // not in patterns
'packages/cli/package.json': pkgJsonContent({ name: '@example/cli' }),
'packages/core/package.json': pkgJsonContent({ name: '@example/core' }),
'scripts/docs/index.js': 'console.log("not yet implemented")', // no package.json
},
MEMFS_VOLUME,
);
await expect(
listPackages(MEMFS_VOLUME, ['packages/*', 'e2e', 'scripts/*']),
).resolves.toEqual([
expect.objectContaining({ name: 'e2e' }),
expect.objectContaining({ name: '@example/cli' }),
expect.objectContaining({ name: '@example/core' }),
]);
});
it('should sort packages by package.json path', async () => {
vol.fromJSON(
{
'package.json': pkgJsonContent({ name: 'example-monorepo' }),
'packages/core/package.json': pkgJsonContent({ name: '@example/core' }),
'e2e/package.json': pkgJsonContent({ name: 'e2e' }),
'packages/cli/package.json': pkgJsonContent({ name: '@example/cli' }),
},
MEMFS_VOLUME,
);
await expect(listPackages(MEMFS_VOLUME)).resolves.toEqual([
expect.objectContaining({ name: 'e2e' }),
expect.objectContaining({ name: 'example-monorepo' }),
expect.objectContaining({ name: '@example/cli' }),
expect.objectContaining({ name: '@example/core' }),
]);
});
it('should use parent folder name if "name" missing in package.json', async () => {
vol.fromJSON(
{
'e2e/package.json': pkgJsonContent({}),
'package.json': pkgJsonContent({}),
'packages/cli/package.json': pkgJsonContent({ name: '@example/cli' }),
'packages/core/package.json': pkgJsonContent({ name: '@example/core' }),
'packages/utils/package.json': pkgJsonContent({}),
},
MEMFS_VOLUME,
);
await expect(listPackages(MEMFS_VOLUME)).resolves.toEqual([
expect.objectContaining({ name: 'e2e' }),
expect.objectContaining({ name: path.basename(MEMFS_VOLUME) }),
expect.objectContaining({ name: '@example/cli' }),
expect.objectContaining({ name: '@example/core' }),
expect.objectContaining({ name: 'utils' }),
]);
});
});
describe('listWorkspaces', () => {
it('should list workspaces named in root package.json', async () => {
vol.fromJSON(
{
'package.json': pkgJsonContent({
private: true,
workspaces: ['ui', 'api'],
}),
'api/package.json': pkgJsonContent({ name: 'api' }),
'e2e/package.json': pkgJsonContent({ name: 'e2e' }), // not in workspaces
'ui/package.json': pkgJsonContent({ name: 'ui' }),
},
MEMFS_VOLUME,
);
await expect(listWorkspaces(MEMFS_VOLUME)).resolves.toEqual({
workspaces: [
{
name: 'api',
directory: path.join(MEMFS_VOLUME, 'api'),
packageJson: { name: 'api' },
},
{
name: 'ui',
directory: path.join(MEMFS_VOLUME, 'ui'),
packageJson: { name: 'ui' },
},
],
rootPackageJson: {
private: true,
workspaces: ['ui', 'api'],
},
});
});
it('should list workspaces matched by glob in root package.json', async () => {
vol.fromJSON(
{
'package.json': pkgJsonContent({
private: true,
workspaces: ['packages/*'],
}),
'e2e/package.json': pkgJsonContent({ name: 'e2e' }), // not in workspaces
'packages/cli/package.json': pkgJsonContent({ name: 'cli' }),
'packages/core/package.json': pkgJsonContent({ name: 'core' }),
},
MEMFS_VOLUME,
);
await expect(listWorkspaces(MEMFS_VOLUME)).resolves.toEqual({
workspaces: [
{
name: 'cli',
directory: path.join(MEMFS_VOLUME, 'packages', 'cli'),
packageJson: { name: 'cli' },
},
{
name: 'core',
directory: path.join(MEMFS_VOLUME, 'packages', 'core'),
packageJson: { name: 'core' },
},
],
rootPackageJson: {
private: true,
workspaces: ['packages/*'],
},
});
});
it('should parse patterns from workspaces object config in root package.json', async () => {
vol.fromJSON(
{
'package.json': pkgJsonContent({
private: true,
workspaces: {
packages: ['apps/*'],
nohoist: ['**/mobile'],
},
}),
'apps/desktop/package.json': pkgJsonContent({ name: 'desktop' }),
'apps/mobile/package.json': pkgJsonContent({ name: 'mobile' }),
},
MEMFS_VOLUME,
);
await expect(listWorkspaces(MEMFS_VOLUME)).resolves.toEqual({
workspaces: [
{
name: 'desktop',
directory: path.join(MEMFS_VOLUME, 'apps', 'desktop'),
packageJson: { name: 'desktop' },
},
{
name: 'mobile',
directory: path.join(MEMFS_VOLUME, 'apps', 'mobile'),
packageJson: { name: 'mobile' },
},
],
rootPackageJson: {
private: true,
workspaces: {
packages: ['apps/*'],
nohoist: ['**/mobile'],
},
},
});
});
});
describe('hasWorkspacesEnabled', () => {
it('should identify as NOT enabled if "workspaces" missing in root package.json', async () => {
vol.fromJSON(
{ 'package.json': pkgJsonContent({ name: 'example', private: true }) },
MEMFS_VOLUME,
);
await expect(hasWorkspacesEnabled(MEMFS_VOLUME)).resolves.toBeFalse();
});
it('should identify as NOT enabled if `"private": true` missing in root package.json', async () => {
vol.fromJSON(
{
'package.json': pkgJsonContent({
name: 'example',
workspaces: ['packages/*'],
}),
},
MEMFS_VOLUME,
);
await expect(hasWorkspacesEnabled(MEMFS_VOLUME)).resolves.toBeFalse();
});
it('should identify as enabled if private and workspaces array set in root package.json', async () => {
vol.fromJSON(
{
'package.json': pkgJsonContent({
private: true,
workspaces: ['packages/*'],
}),
},
MEMFS_VOLUME,
);
await expect(hasWorkspacesEnabled(MEMFS_VOLUME)).resolves.toBeTrue();
});
it('should identify as enabled if private and workspaces object set in root package.json', async () => {
vol.fromJSON(
{
'package.json': pkgJsonContent({
private: true,
workspaces: {
packages: ['packages/*'],
nohoist: ['**/react-native'],
},
}),
},
MEMFS_VOLUME,
);
await expect(hasWorkspacesEnabled(MEMFS_VOLUME)).resolves.toBeTrue();
});
});
describe('readRootPackageJson', () => {
it('should read and parse package.json from current working directory', async () => {
vol.fromJSON(
{ 'package.json': pkgJsonContent({ name: 'example' }) },
MEMFS_VOLUME,
);
await expect(readRootPackageJson(MEMFS_VOLUME)).resolves.toEqual({
name: 'example',
});
});
it("should throw if root package.json doesn't exist", async () => {
vol.fromJSON(
{
'api/package.json': pkgJsonContent({ name: 'api' }),
'ui/package.json': pkgJsonContent({ name: 'ui' }),
},
MEMFS_VOLUME,
);
await expect(readRootPackageJson(MEMFS_VOLUME)).rejects.toThrow(
'no such file or directory',
);
});
it("should throw if package.json exists but isn't valid JSON", async () => {
vol.fromJSON({ 'package.json': '' }, MEMFS_VOLUME);
await expect(readRootPackageJson(MEMFS_VOLUME)).rejects.toThrow(
'Unexpected end of JSON input',
);
});
});
describe('hasScript', () => {
it('should return true if script in package.json "scripts"', () => {
expect(
hasScript(
{ scripts: { 'code-pushup': 'code-pushup --no-progress' } },
'code-pushup',
),
).toBeTrue();
});
it('should return false if script not in package.json "scripts"', () => {
expect(hasScript({}, 'code-pushup')).toBeFalse();
});
});
describe('hasDependency', () => {
it('should return true if package name in "dependencies"', () => {
expect(
hasDependency({ dependencies: { react: '^19.0.0' } }, 'react'),
).toBeTrue();
});
it('should return true if package name in "devDependencies"', () => {
expect(
hasDependency({ devDependencies: { nx: '20.1.3' } }, 'nx'),
).toBeTrue();
});
it('should return false if package name is neither in "dependencies" nor "devDependencies"', () => {
expect(
hasDependency(
{
dependencies: { react: '^19.0.0' },
devDependencies: { typescript: '5.5.4' },
},
'svelte',
),
).toBeFalse();
});
});
describe('hasCodePushUpDependency', () => {
it('should return true if @code-pushup/cli in "devDependencies"', () => {
expect(
hasCodePushUpDependency({
devDependencies: { '@code-pushup/cli': '^0.55.0' },
}),
).toBeTrue();
});
it('should return true if @code-pushup/cli in "dependencies"', () => {
expect(
hasCodePushUpDependency({
dependencies: { '@code-pushup/cli': 'latest' },
}),
).toBeTrue();
});
it('should return false if @code-pushup/cli is neither in "dependencies" nor "devDependencies"', () => {
expect(
hasCodePushUpDependency({
dependencies: {
'@code-pushup/models': 'latest',
'@code-pushup/utils': 'latest',
},
}),
).toBeFalse();
});
});