-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy patheslint-plugin.int.test.ts
More file actions
177 lines (161 loc) · 5.36 KB
/
Copy patheslint-plugin.int.test.ts
File metadata and controls
177 lines (161 loc) · 5.36 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
import ansis from 'ansis';
import { cp } from 'node:fs/promises';
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 } from '@code-pushup/models';
import {
restoreNxIgnoredFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { eslintPlugin } from './eslint-plugin.js';
describe('eslintPlugin', () => {
const thisDir = fileURLToPath(path.dirname(import.meta.url));
const fixturesDir = path.join(thisDir, '..', '..', 'mocks', 'fixtures');
const tmpDir = path.join(process.cwd(), 'tmp', 'int', 'plugin-eslint');
let cwdSpy: MockInstance<[], string>;
let platformSpy: MockInstance<[], NodeJS.Platform>;
beforeAll(async () => {
await cp(
path.join(fixturesDir, 'nx-monorepo'),
path.join(tmpDir, 'nx-monorepo'),
{ recursive: true },
);
await restoreNxIgnoredFiles(path.join(tmpDir, 'nx-monorepo'));
await cp(
path.join(fixturesDir, 'todos-app'),
path.join(tmpDir, 'todos-app'),
{ recursive: true },
);
await restoreNxIgnoredFiles(path.join(tmpDir, 'todos-app'));
cwdSpy = vi.spyOn(process, 'cwd');
// Linux produces extra quotation marks for globs
platformSpy = vi.spyOn(os, 'platform').mockReturnValue('linux');
});
afterAll(async () => {
cwdSpy.mockRestore();
platformSpy.mockRestore();
await teardownTestFolder(tmpDir);
});
it('should initialize ESLint plugin for React application', async () => {
cwdSpy.mockReturnValue(path.join(tmpDir, 'todos-app'));
const plugin = await eslintPlugin({
eslintrc: 'eslint.config.js',
patterns: ['src/**/*.js', 'src/**/*.jsx'],
});
expect(plugin).toMatchSnapshot({
version: expect.any(String),
});
});
it('should initialize ESLint plugin for Nx project', async () => {
cwdSpy.mockReturnValue(path.join(tmpDir, '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).toStrictEqual(
expect.arrayContaining([
expect.objectContaining<Audit>({
slug: expect.stringMatching(/^nx-enforce-module-boundaries/),
title: expect.any(String),
description: expect.stringContaining('sourceTag'),
}),
expect.objectContaining<Partial<Audit>>({
slug: 'nx-nx-plugin-checks',
}),
]),
);
});
it('should initialize with plugin options for custom groups', async () => {
cwdSpy.mockReturnValue(path.join(tmpDir, '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('Invalid input');
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('Invalid input');
});
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 ${ansis.bold('ESLintPluginConfig')}`);
});
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(tmpDir, 'todos-app'));
const plugin = await eslintPlugin(
{
eslintrc: 'eslint.config.js',
patterns: ['src/**/*.js'],
},
{
artifacts: {
artifactsPaths: './artifacts/eslint-output.json',
generateArtifactsCommand: 'echo "Generating artifacts"',
},
},
);
expect(plugin.runner).toBeTypeOf('function');
});
});