-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathjs-packages-plugin.unit.test.ts
More file actions
190 lines (179 loc) · 5.59 KB
/
js-packages-plugin.unit.test.ts
File metadata and controls
190 lines (179 loc) · 5.59 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
import { vol } from 'memfs';
import { describe, expect, it } from 'vitest';
import {
type Group,
type PluginConfig,
pluginConfigSchema,
} from '@code-pushup/models';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import { jsPackagesPlugin } from './js-packages-plugin.js';
describe('jsPackagesPlugin', () => {
it('should initialise the plugin without a given config for NPM', async () => {
vol.fromJSON(
{
'package.json': JSON.stringify({
packageManager:
'npm@1.2.3+sha224.953c8233f7a92884eee2de69a1b92d1f2ec1655e66d08071ba9a02fa',
}),
},
MEMFS_VOLUME,
);
await expect(jsPackagesPlugin()).resolves.toStrictEqual(
expect.objectContaining({
slug: 'js-packages',
title: 'JS Packages',
audits: expect.arrayContaining([
expect.objectContaining({ slug: 'npm-audit-prod' }),
expect.objectContaining({ slug: 'npm-audit-dev' }),
expect.objectContaining({ slug: 'npm-outdated-dev' }),
]),
groups: expect.any(Array),
}),
);
});
it('should initialise the plugin with given checks', async () => {
vol.fromJSON(
{
'package.json': JSON.stringify({
packageManager:
'npm@1.2.3+sha224.953c8233f7a92884eee2de69a1b92d1f2ec1655e66d08071ba9a02fa',
}),
},
MEMFS_VOLUME,
);
await expect(
jsPackagesPlugin({ checks: ['outdated'] }),
).resolves.toStrictEqual(
expect.objectContaining({
slug: 'js-packages',
title: 'JS Packages',
audits: expect.any(Array),
groups: expect.any(Array),
runner: expect.any(Function),
}),
);
});
it('should create a group for both audit and outdated when no check configuration is provided', async () => {
vol.fromJSON(
{
'package.json': JSON.stringify({
packageManager:
'npm@1.2.3+sha224.953c8233f7a92884eee2de69a1b92d1f2ec1655e66d08071ba9a02fa',
}),
},
MEMFS_VOLUME,
);
await expect(jsPackagesPlugin()).resolves.toStrictEqual(
expect.objectContaining<Partial<PluginConfig>>({
groups: [
expect.objectContaining<Partial<Group>>({
slug: 'npm-audit',
}),
expect.objectContaining<Partial<Group>>({
slug: 'npm-outdated',
}),
],
}),
);
});
it('should configure a group based on package manager and chosen check', async () => {
await expect(
jsPackagesPlugin({ packageManager: 'yarn-modern', checks: ['outdated'] }),
).resolves.toStrictEqual(
expect.objectContaining({
groups: [
expect.objectContaining<Partial<Group>>({
slug: 'yarn-modern-outdated',
}),
],
}),
);
});
it('should create an audit for default dependency groups', async () => {
await expect(
jsPackagesPlugin({
packageManager: 'yarn-classic',
checks: ['audit'],
}),
).resolves.toStrictEqual(
expect.objectContaining({
audits: [
expect.objectContaining({ slug: 'yarn-classic-audit-prod' }),
expect.objectContaining({ slug: 'yarn-classic-audit-dev' }),
],
groups: [
expect.objectContaining<Partial<Group>>({
slug: 'yarn-classic-audit',
refs: [
{ slug: 'yarn-classic-audit-prod', weight: 80 },
{ slug: 'yarn-classic-audit-dev', weight: 15 },
],
}),
],
}),
);
});
it('should create an audit for selected dependency groups', async () => {
await expect(
jsPackagesPlugin({
packageManager: 'yarn-classic',
checks: ['audit'],
dependencyGroups: ['prod', 'optional'],
}),
).resolves.toStrictEqual(
expect.objectContaining({
audits: [
expect.objectContaining({ slug: 'yarn-classic-audit-prod' }),
expect.objectContaining({ slug: 'yarn-classic-audit-optional' }),
],
groups: [
expect.objectContaining<Partial<Group>>({
slug: 'yarn-classic-audit',
refs: [
{ slug: 'yarn-classic-audit-prod', weight: 80 },
{ slug: 'yarn-classic-audit-optional', weight: 5 },
],
}),
],
}),
);
});
// Note: Yarn v2 does not support audit for optional dependencies
it('should omit unsupported dependency groups', async () => {
await expect(
jsPackagesPlugin({
packageManager: 'yarn-modern',
checks: ['audit'],
dependencyGroups: ['prod', 'optional'],
}),
).resolves.toStrictEqual(
expect.objectContaining({
audits: [expect.objectContaining({ slug: 'yarn-modern-audit-prod' })],
groups: [
expect.objectContaining<Partial<Group>>({
slug: 'yarn-modern-audit',
refs: [{ slug: 'yarn-modern-audit-prod', weight: 80 }],
}),
],
}),
);
});
it('should use an icon that matches the chosen package manager', async () => {
await expect(
jsPackagesPlugin({ packageManager: 'pnpm' }),
).resolves.toStrictEqual(
expect.objectContaining<Partial<PluginConfig>>({
icon: 'pnpm',
}),
);
});
it('should pass scoreTargets to PluginConfig when provided', async () => {
const scoreTargets = { 'npm-outdated-dev': 0.9 };
const pluginConfig = await jsPackagesPlugin({
packageManager: 'npm',
scoreTargets,
});
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow();
expect(pluginConfig.scoreTargets).toStrictEqual(scoreTargets);
});
});