-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpackage-json.plugin.int.test.ts
More file actions
204 lines (186 loc) · 5.77 KB
/
package-json.plugin.int.test.ts
File metadata and controls
204 lines (186 loc) · 5.77 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
import { vol } from 'memfs';
import { describe, expect, it } from 'vitest';
import { executePlugin } from '@code-pushup/core';
import {
auditSchema,
categoryRefSchema,
pluginConfigSchema,
pluginReportSchema,
} from '@code-pushup/models';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import { audits, pluginSlug as slug } from './constants.js';
import { type PluginOptions, create } from './package-json.plugin.js';
import {
documentationGroupRef,
performanceGroupRef,
recommendedRefs,
versionControlGroupRef,
} from './scoring.js';
describe('create-package-json', () => {
const baseOptions: PluginOptions = {
directory: '/',
};
beforeEach(() => {
vol.fromJSON(
{
'package.json': '{}',
},
MEMFS_VOLUME,
);
});
it('should return valid PluginConfig', () => {
const pluginConfig = create(baseOptions);
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow();
expect(pluginConfig).toEqual({
slug,
description: 'A plugin to validate package.json files.',
icon: 'npm',
runner: expect.any(Function),
title: 'Package Json',
audits: expect.arrayContaining(audits),
groups: expect.any(Array),
});
});
it('should return PluginConfig that executes correctly', async () => {
const pluginConfig = create(baseOptions);
const pluginOutput = await executePlugin(pluginConfig, {
persist: { outputDir: '.code-pushup' },
cache: { read: false, write: false },
});
expect(() => pluginReportSchema.parse(pluginOutput)).not.toThrow();
expect(pluginOutput).toMatchObject(
expect.objectContaining({
slug,
description: 'A plugin to validate package.json files.',
icon: 'npm',
duration: expect.any(Number),
date: expect.any(String),
title: 'Package Json',
groups: expect.any(Array),
}),
);
});
it('should use license', async () => {
const pluginConfig = create({
...baseOptions,
license: 'MIT',
});
const { audits: auditOutputs } = await executePlugin(pluginConfig, {
persist: { outputDir: '.code-pushup' },
cache: { read: false, write: false },
});
expect(auditOutputs[0]?.value).toBe(1);
expect(auditOutputs[0]?.score).toBe(0);
expect(auditOutputs[0]?.details?.issues).toEqual([
{
message: 'license should be MIT but is undefined',
severity: 'error',
},
]);
});
it('should use type', async () => {
const pluginConfig = create({
...baseOptions,
type: 'module',
});
const { audits: auditOutputs } = await executePlugin(pluginConfig, {
persist: { outputDir: '.code-pushup' },
cache: { read: false, write: false },
});
expect(auditOutputs[1]?.slug).toBe('package-type');
expect(auditOutputs[1]?.score).toBe(0);
expect(auditOutputs[1]?.details?.issues).toEqual([
{
message: 'type should be module but is undefined',
severity: 'error',
},
]);
});
it('should use dependencies', async () => {
const pluginConfig = create({
...baseOptions,
dependencies: {
test: '0',
},
});
const { audits: auditOutputs } = await executePlugin(pluginConfig, {
persist: { outputDir: '.code-pushup' },
cache: { read: false, write: false },
});
expect(auditOutputs).toHaveLength(audits.length);
expect(auditOutputs[2]?.slug).toBe('package-dependencies');
expect(auditOutputs[2]?.score).toBe(0);
expect(auditOutputs[2]?.details?.issues).toEqual([
{
message:
'Package test is not installed under dependencies. Run `npm install test@0` to install it.',
severity: 'error',
},
]);
});
it('should use optionalDependencies', async () => {
const pluginConfig = create({
...baseOptions,
optionalDependencies: {
test: '0',
},
});
const { audits: auditOutputs } = await executePlugin(pluginConfig, {
persist: { outputDir: '.code-pushup' },
cache: { read: false, write: false },
});
expect(auditOutputs).toHaveLength(audits.length);
expect(auditOutputs[2]?.score).toBe(0);
expect(auditOutputs[2]?.slug).toBe('package-dependencies');
expect(auditOutputs[2]?.details?.issues).toEqual([
{
message:
'Package test is not installed under optionalDependencies. Run `npm install test@0` to install it.',
severity: 'error',
},
]);
});
it('should use devDependencies', async () => {
const pluginConfig = create({
...baseOptions,
devDependencies: {
test: '0',
},
});
const { audits: auditOutputs } = await executePlugin(pluginConfig, {
persist: { outputDir: '.code-pushup' },
cache: { read: false, write: false },
});
expect(auditOutputs).toHaveLength(audits.length);
expect(auditOutputs[2]?.score).toBe(0);
expect(auditOutputs[2]?.slug).toBe('package-dependencies');
expect(auditOutputs[2]?.details?.issues).toEqual([
{
message:
'Package test is not installed under devDependencies. Run `npm install test@0` to install it.',
severity: 'error',
},
]);
});
});
describe('audits', () => {
it.each(audits)('should be a valid audit meta info', audit => {
expect(() => auditSchema.parse(audit)).not.toThrow();
});
});
describe('groupRefs', () => {
it.each([versionControlGroupRef, performanceGroupRef, documentationGroupRef])(
'should be a valid category reference',
groupRef => {
expect(() => categoryRefSchema.parse(groupRef)).not.toThrow();
},
);
});
describe('recommendedRefs', () => {
it.each(recommendedRefs)(
'should be a valid category reference',
categoryRef => {
expect(() => categoryRefSchema.parse(categoryRef)).not.toThrow();
},
);
});