-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.unit.test.ts
More file actions
152 lines (141 loc) · 4.03 KB
/
index.unit.test.ts
File metadata and controls
152 lines (141 loc) · 4.03 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
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { z } from 'zod';
import {
type Audit,
type AuditOutput,
DEFAULT_PERSIST_CONFIG,
type pluginArtifactOptionsSchema,
} from '@code-pushup/models';
import { logger } from '@code-pushup/utils';
import type { ESLintTarget } from '../config.js';
import { createRunnerFunction } from './index.js';
import * as lintModule from './lint.js';
import type { LinterOutput } from './types.js';
import * as utilsFileModule from './utils.js';
describe('createRunnerFunction', () => {
const loadArtifactsSpy = vi.spyOn(utilsFileModule, 'loadArtifacts');
const lintSpy = vi.spyOn(lintModule, 'lint');
const mockAudits: Audit[] = [
{ slug: 'max-lines', title: 'Max lines', description: 'Test' },
{ slug: 'no-unused-vars', title: 'No unused vars', description: 'Test' },
];
const mockTargetPatterns = { patterns: ['src/**/*.ts'] };
const mockTargetPatternsAndConfigs = {
patterns: ['lib/**/*.js'],
eslintrc: '.eslintrc.js',
};
const mockTargets: ESLintTarget[] = [
mockTargetPatterns,
mockTargetPatternsAndConfigs,
];
const mockLinterOutputs: LinterOutput[] = [
{
results: [
{
filePath: 'test.ts',
messages: [
{
ruleId: 'max-lines',
severity: 1,
message: 'File has too many lines',
line: 1,
column: 1,
},
],
} as any,
],
ruleOptionsPerFile: { 'test.ts': { 'max-lines': [] } },
},
{
results: [
{
filePath: 'test.ts',
messages: [
{
ruleId: 'max-lines',
severity: 1,
message: 'File has too many lines',
line: 1,
column: 1,
},
],
} as any,
],
ruleOptionsPerFile: { 'test.ts': { 'max-lines': [] } },
},
];
const mockedAuditOutputs: AuditOutput[] = [
{
slug: 'max-lines',
score: 0,
value: 2,
displayValue: '2 warnings',
details: {
issues: [
{
message: 'File has too many lines',
severity: 'warning',
source: {
file: 'test.ts',
position: {
startLine: 1,
startColumn: 1,
},
},
},
{
message: 'File has too many lines',
severity: 'warning',
source: {
file: 'test.ts',
position: {
startLine: 1,
startColumn: 1,
},
},
},
],
},
},
{
slug: 'no-unused-vars',
score: 1,
value: 0,
displayValue: 'passed',
details: { issues: [] },
},
];
beforeEach(() => {
vi.clearAllMocks();
});
it('should use loadArtifacts when artifacts are provided', async () => {
const artifacts: z.infer<typeof pluginArtifactOptionsSchema> = {
artifactsPaths: ['path/to/artifacts.json'],
};
loadArtifactsSpy.mockResolvedValue(mockLinterOutputs);
await expect(
createRunnerFunction({
audits: mockAudits,
targets: mockTargets,
artifacts,
})({ persist: DEFAULT_PERSIST_CONFIG }),
).resolves.toStrictEqual(mockedAuditOutputs);
expect(loadArtifactsSpy).toHaveBeenCalledWith(artifacts);
expect(lintSpy).not.toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledWith(
'ESLint plugin executing 2 lint targets',
);
});
it('should use internal linting logic when artifacts are not provided', async () => {
lintSpy.mockResolvedValueOnce(mockLinterOutputs.at(0)!);
lintSpy.mockResolvedValueOnce(mockLinterOutputs.at(0)!);
await expect(
createRunnerFunction({
audits: mockAudits,
targets: mockTargets,
})({ persist: DEFAULT_PERSIST_CONFIG }),
).resolves.toStrictEqual(mockedAuditOutputs);
expect(loadArtifactsSpy).not.toHaveBeenCalled();
expect(lintSpy).toHaveBeenCalledTimes(2);
});
});