-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrunner.unit.test.ts
More file actions
232 lines (214 loc) · 6.29 KB
/
Copy pathrunner.unit.test.ts
File metadata and controls
232 lines (214 loc) · 6.29 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
import { vol } from 'memfs';
import {
type AuditOutputs,
DEFAULT_PERSIST_CONFIG,
DEFAULT_PERSIST_FILENAME,
DEFAULT_PERSIST_FORMAT,
DEFAULT_PERSIST_OUTPUT_DIR,
DEFAULT_PERSIST_SKIP_REPORT,
auditOutputsSchema,
} from '@code-pushup/models';
import {
ISO_STRING_REGEXP,
MEMFS_VOLUME,
MINIMAL_PLUGIN_CONFIG_MOCK,
MINIMAL_RUNNER_CONFIG_MOCK,
MINIMAL_RUNNER_FUNCTION_MOCK,
osAgnosticPath,
} from '@code-pushup/test-utils';
import * as utils from '@code-pushup/utils';
import {
type RunnerResult,
executePluginRunner,
executeRunnerConfig,
executeRunnerFunction,
getRunnerOutputsPath,
} from './runner.js';
describe('getRunnerOutputsPath', () => {
it('should read runner results from a file', async () => {
expect(
osAgnosticPath(getRunnerOutputsPath('plugin-with-cache', 'output')),
).toBe(osAgnosticPath('output/plugin-with-cache/runner-output.json'));
});
});
describe('executeRunnerConfig', () => {
beforeEach(() => {
vol.fromJSON(
{
'output.json': JSON.stringify([
{
slug: 'node-version',
score: 0.3,
value: 16,
},
]),
},
MEMFS_VOLUME,
);
vi.spyOn(utils, 'executeProcess');
});
it('should execute valid runner config', async () => {
const runnerResult = await executeRunnerConfig(MINIMAL_RUNNER_CONFIG_MOCK, {
persist: DEFAULT_PERSIST_CONFIG,
});
// data sanity
expect((runnerResult.audits as AuditOutputs)[0]?.slug).toBe('node-version');
expect(runnerResult.date).toMatch(ISO_STRING_REGEXP);
expect(runnerResult.duration).toBeGreaterThanOrEqual(0);
// schema validation
expect(() => auditOutputsSchema.parse(runnerResult.audits)).not.toThrow();
// executed process configuration
expect(utils.executeProcess).toHaveBeenCalledWith<[utils.ProcessConfig]>({
command: 'node',
args: ['-v'],
env: expect.objectContaining({
CP_PERSIST_OUTPUT_DIR: DEFAULT_PERSIST_OUTPUT_DIR,
CP_PERSIST_FILENAME: DEFAULT_PERSIST_FILENAME,
CP_PERSIST_FORMAT: DEFAULT_PERSIST_FORMAT.join(','),
CP_PERSIST_SKIP_REPORTS: `${DEFAULT_PERSIST_SKIP_REPORT}`,
}),
observer: {
onStdout: expect.any(Function),
onStderr: expect.any(Function),
},
verbose: false,
});
});
it('should use outputTransform when provided', async () => {
const runnerResult = await executeRunnerConfig(
{
command: 'node',
args: ['-v'],
outputFile: 'output.json',
outputTransform: (outputs: unknown): Promise<AuditOutputs> =>
Promise.resolve([
{
slug: (outputs as AuditOutputs)[0]!.slug,
score: 0.3,
value: 16,
displayValue: '16.0.0',
},
]),
},
{ persist: DEFAULT_PERSIST_CONFIG },
);
const auditOutputs = runnerResult.audits as AuditOutputs;
expect(auditOutputs[0]?.slug).toBe('node-version');
expect(auditOutputs[0]?.displayValue).toBe('16.0.0');
});
it('should throw if outputTransform throws', async () => {
await expect(
executeRunnerConfig(
{
command: 'node',
args: ['-v'],
outputFile: 'output.json',
outputTransform: () =>
Promise.reject(new Error('Error: outputTransform has failed.')),
},
{ persist: DEFAULT_PERSIST_CONFIG },
),
).rejects.toThrow('Error: outputTransform has failed.');
});
});
describe('executeRunnerFunction', () => {
it('should execute a valid runner function', async () => {
const runnerResult: RunnerResult = await executeRunnerFunction(
MINIMAL_RUNNER_FUNCTION_MOCK,
{ persist: DEFAULT_PERSIST_CONFIG },
);
const auditOutputs = runnerResult.audits as AuditOutputs;
expect(auditOutputs[0]?.slug).toBe('node-version');
expect(auditOutputs[0]?.details?.issues).toEqual([
expect.objectContaining({
message: 'The required Node version to run Code PushUp CLI is 18.',
}),
]);
});
it('should throw if the runner function throws', async () => {
await expect(
executeRunnerFunction(
() => Promise.reject(new Error('Error: Runner has failed.')),
{ persist: DEFAULT_PERSIST_CONFIG },
),
).rejects.toThrow('Error: Runner has failed.');
});
it('should throw with an invalid runner type', async () => {
await expect(
// @ts-expect-error Testing a use case with invalid type passed as a function.
executeRunnerFunction(''),
).rejects.toThrow('runner is not a function');
});
});
describe('executePluginRunner', () => {
it('should execute a valid plugin config', async () => {
const pluginResult = await executePluginRunner(MINIMAL_PLUGIN_CONFIG_MOCK, {
persist: DEFAULT_PERSIST_CONFIG,
});
expect(pluginResult.audits[0]?.slug).toBe('node-version');
});
it('should yield audit outputs for valid runner config', async () => {
vol.fromJSON(
{
'output.json': JSON.stringify([
{
slug: 'node-version',
score: 0.3,
value: 16,
},
]),
},
MEMFS_VOLUME,
);
await expect(
executePluginRunner(
{
...MINIMAL_PLUGIN_CONFIG_MOCK,
runner: {
command: 'node',
args: ['-v'],
outputFile: 'output.json',
},
},
{ persist: DEFAULT_PERSIST_CONFIG },
),
).resolves.toStrictEqual({
duration: expect.any(Number),
date: expect.any(String),
audits: expect.arrayContaining([
{
slug: 'node-version',
score: 0.3,
value: 16,
},
]),
});
});
it('should yield audit outputs for a valid runner function', async () => {
await expect(
executePluginRunner(
{
...MINIMAL_PLUGIN_CONFIG_MOCK,
runner: () => [
{
slug: 'node-version',
score: 0.3,
value: 16,
},
],
},
{ persist: DEFAULT_PERSIST_CONFIG },
),
).resolves.toStrictEqual({
duration: expect.any(Number),
date: expect.any(String),
audits: expect.arrayContaining([
{
slug: 'node-version',
score: 0.3,
value: 16,
},
]),
});
});
});