-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy patheslint-plugin.int.test.ts
More file actions
189 lines (171 loc) · 5.94 KB
/
Copy patheslint-plugin.int.test.ts
File metadata and controls
189 lines (171 loc) · 5.94 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
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import type { MockInstance } from 'vitest';
import type { Audit, PluginConfig, RunnerConfig } from '@code-pushup/models';
import { toUnixPath } from '@code-pushup/utils';
import { eslintPlugin } from './eslint-plugin.js';
describe('eslintPlugin', () => {
const thisDir = fileURLToPath(path.dirname(import.meta.url));
const fixturesDir = path.join(thisDir, '..', '..', 'mocks', 'fixtures');
let cwdSpy: MockInstance<[], string>;
let platformSpy: MockInstance<[], NodeJS.Platform>;
const replaceAbsolutePath = (plugin: PluginConfig): PluginConfig => ({
...plugin,
runner: {
...(plugin.runner as RunnerConfig),
args: (plugin.runner as RunnerConfig).args?.map(arg =>
toUnixPath(arg.replace(path.dirname(thisDir), '<dirname>')).replace(
/\/eslint\/\d+\//,
'/eslint/<timestamp>/',
),
),
...((plugin.runner as RunnerConfig).configFile && {
configFile: toUnixPath(
(plugin.runner as RunnerConfig).configFile!,
).replace(/\/eslint\/\d+\//, '/eslint/<timestamp>/'),
}),
outputFile: toUnixPath(
(plugin.runner as RunnerConfig).outputFile,
).replace(/\/eslint\/\d+\//, '/eslint/<timestamp>/'),
},
});
beforeAll(() => {
cwdSpy = vi.spyOn(process, 'cwd');
// Linux produces extra quotation marks for globs
platformSpy = vi.spyOn(os, 'platform').mockReturnValue('linux');
});
afterAll(() => {
cwdSpy.mockRestore();
platformSpy.mockRestore();
});
it('should initialize ESLint plugin for React application', async () => {
cwdSpy.mockReturnValue(path.join(fixturesDir, 'todos-app'));
const plugin = await eslintPlugin({
eslintrc: 'eslint.config.js',
patterns: ['src/**/*.js', 'src/**/*.jsx'],
});
expect(replaceAbsolutePath(plugin)).toMatchSnapshot({
version: expect.any(String),
});
});
it('should initialize ESLint plugin for Nx project', async () => {
cwdSpy.mockReturnValue(path.join(fixturesDir, 'nx-monorepo'));
const plugin = await eslintPlugin({
eslintrc: './packages/nx-plugin/eslint.config.js',
patterns: ['packages/nx-plugin/**/*.ts', 'packages/nx-plugin/**/*.json'],
});
// expect rule from extended base eslint.config.js
expect(plugin.audits).toContainEqual(
expect.objectContaining<Audit>({
slug: expect.stringMatching(/^nx-enforce-module-boundaries/),
title: expect.any(String),
description: expect.stringContaining('sourceTag'),
}),
);
// expect rule from nx-plugin project's eslint.config.js
expect(plugin.audits).toContainEqual(
expect.objectContaining<Partial<Audit>>({
slug: 'nx-nx-plugin-checks',
}),
);
});
it('should initialize with plugin options for custom groups', async () => {
cwdSpy.mockReturnValue(path.join(fixturesDir, 'nx-monorepo'));
const plugin = await eslintPlugin(
{
eslintrc: './packages/nx-plugin/eslint.config.js',
patterns: ['packages/nx-plugin/**/*.ts'],
},
{
groups: [
{
slug: 'type-safety',
title: 'Type safety',
rules: [
'@typescript-eslint/no-explicit-any',
'@typescript-eslint/no-unsafe-*',
],
},
],
},
);
expect(plugin.groups).toContainEqual({
slug: 'type-safety',
title: 'Type safety',
refs: [
{ slug: 'typescript-eslint-no-explicit-any', weight: 1 },
{
slug: 'typescript-eslint-no-unsafe-declaration-merging',
weight: 1,
},
{ slug: 'typescript-eslint-no-unsafe-function-type', weight: 1 },
],
});
expect(plugin.audits).toContainEqual(
expect.objectContaining<Partial<Audit>>({
slug: 'typescript-eslint-no-explicit-any',
}),
);
});
it('should throw when custom group rules are empty', async () => {
await expect(
eslintPlugin(
{
eslintrc: './packages/nx-plugin/eslint.config.js',
patterns: ['packages/nx-plugin/**/*.ts'],
},
{
groups: [{ slug: 'type-safety', title: 'Type safety', rules: [] }],
},
),
).rejects.toThrow(/Custom group rules must contain at least 1 element/);
await expect(
eslintPlugin(
{
eslintrc: './packages/nx-plugin/eslint.config.js',
patterns: ['packages/nx-plugin/**/*.ts'],
},
{
groups: [{ slug: 'type-safety', title: 'Type safety', rules: {} }],
},
),
).rejects.toThrow(/Custom group rules must contain at least 1 element/);
});
it('should throw when invalid parameters provided', async () => {
await expect(
// @ts-expect-error simulating invalid non-TS config
eslintPlugin({ eslintrc: '.eslintrc.json' }),
).rejects.toThrow(/Invalid input/);
});
it("should throw if eslintrc file doesn't exist", async () => {
await expect(
eslintPlugin({ eslintrc: '.eslintrc.yml', patterns: '**/*.js' }),
).rejects.toThrow(/Failed to load url .*\.eslintrc.yml/);
});
it('should initialize with artifact options', async () => {
cwdSpy.mockReturnValue(path.join(fixturesDir, 'todos-app'));
const plugin = await eslintPlugin(
{
eslintrc: 'eslint.config.js',
patterns: ['src/**/*.js'],
},
{
artifacts: {
artifactsPaths: './artifacts/eslint-output.json',
generateArtifactsCommand: 'echo "Generating artifacts"',
},
},
);
expect(typeof plugin.runner).toBe('object');
const runnerConfig = plugin.runner as {
command: string;
args?: string[];
outputFile: string;
};
expect(runnerConfig.command).toBe('node');
expect(runnerConfig.args).toContain('echo "Generating artifacts"');
expect(runnerConfig.outputFile).toBe('./artifacts/eslint-output.json');
});
});