-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathrules.int.test.ts
More file actions
178 lines (159 loc) · 5.7 KB
/
rules.int.test.ts
File metadata and controls
178 lines (159 loc) · 5.7 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
import { cp } from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
import type { MockInstance } from 'vitest';
import {
restoreNxIgnoredFiles,
teardownTestFolder,
} from '@code-pushup/test-utils';
import type { ESLintTarget } from '../config.js';
import type { RuleData } from './parse.js';
import { listRules } from './rules.js';
describe('listRules', () => {
const fixturesDir = path.join(
fileURLToPath(path.dirname(import.meta.url)),
'..',
'..',
'..',
'mocks',
'fixtures',
);
const tmpDir = path.join(process.cwd(), 'tmp', 'int', 'plugin-eslint');
let cwdSpy: MockInstance<[], string>;
beforeAll(() => {
cwdSpy = vi.spyOn(process, 'cwd');
});
afterAll(async () => {
cwdSpy.mockRestore();
await teardownTestFolder(tmpDir);
});
describe('React app', () => {
const appRootDir = path.join(tmpDir, 'todos-app');
const eslintrc = path.join(appRootDir, 'eslint.config.js');
const patterns = ['src/**/*.js', 'src/**/*.jsx'];
const targets: ESLintTarget[] = [{ eslintrc, patterns }];
beforeAll(async () => {
await cp(path.join(fixturesDir, 'todos-app'), appRootDir, {
recursive: true,
});
await restoreNxIgnoredFiles(appRootDir);
cwdSpy.mockReturnValue(appRootDir);
});
it('should list expected number of rules', async () => {
await expect(listRules(targets)).resolves.toHaveLength(31);
});
it('should include explicitly set built-in rule', async () => {
await expect(listRules(targets)).resolves.toContainEqual({
id: 'no-const-assign',
meta: {
docs: {
description: 'Disallow reassigning `const` variables',
recommended: true,
url: 'https://eslint.org/docs/latest/rules/no-const-assign',
},
messages: {
const: "'{{name}}' is constant.",
},
schema: [],
type: 'problem',
},
options: [],
} satisfies RuleData);
});
it('should include explicitly set plugin rule', async () => {
await expect(listRules(targets)).resolves.toContainEqual({
id: 'react/jsx-key',
meta: {
docs: {
category: 'Possible Errors',
description:
'Disallow missing `key` props in iterators/collection literals',
recommended: true,
url: 'https://github.com/jsx-eslint/eslint-plugin-react/tree/master/docs/rules/jsx-key.md',
},
messages: expect.any(Object),
schema: [
{
additionalProperties: false,
properties: {
checkFragmentShorthand: { default: false, type: 'boolean' },
checkKeyMustBeforeSpread: { default: false, type: 'boolean' },
warnOnDuplicates: { default: false, type: 'boolean' },
},
type: 'object',
},
],
},
options: [],
} satisfies RuleData);
});
});
describe('Nx monorepo project', () => {
const nxRootDir = path.join(tmpDir, 'nx-monorepo');
const eslintrc = path.join(nxRootDir, 'packages/utils/eslint.config.js');
const patterns = ['packages/utils/**/*.ts', 'packages/utils/**/*.json'];
const targets: ESLintTarget[] = [{ eslintrc, patterns }];
beforeAll(async () => {
await cp(path.join(fixturesDir, 'nx-monorepo'), nxRootDir, {
recursive: true,
});
await restoreNxIgnoredFiles(nxRootDir);
cwdSpy.mockReturnValue(nxRootDir);
});
it('should list expected number of rules', async () => {
const rules = await listRules(targets);
expect(rules.length).toBeGreaterThanOrEqual(50);
});
it('should include explicitly set plugin rule with custom options', async () => {
// set in root eslint.config.js
await expect(listRules(targets)).resolves.toContainEqual({
id: '@nx/enforce-module-boundaries',
meta: expect.any(Object),
options: [
expect.objectContaining({
depConstraints: [
{
onlyDependOnLibsWithTags: ['*'],
sourceTag: '*',
},
],
enforceBuildableLibDependency: true,
}),
],
} satisfies RuleData);
});
it('should include built-in rule set implicitly by extending recommended config', async () => {
// extended via @nx/typescript -> @typescript-eslint/eslint-recommended
await expect(listRules(targets)).resolves.toContainEqual({
id: 'no-var',
meta: expect.any(Object),
options: [],
} as RuleData);
});
it('should include plugin rule set implicitly by extending recommended config', async () => {
// extended via @nx/typescript -> @typescript-eslint/recommended
await expect(listRules(targets)).resolves.toContainEqual({
id: '@typescript-eslint/no-unused-vars',
meta: expect.any(Object),
options: [],
} satisfies RuleData);
});
it('should not include rule which was turned off in extended config', async () => {
// extended TypeScript config sets "no-unused-semi": "off"
await expect(listRules(targets)).resolves.not.toContainEqual(
expect.objectContaining({
id: 'no-unused-expressions',
} satisfies Partial<RuleData>),
);
});
it('should include rule added to root config by project config', async () => {
// set only in packages/utils/eslint.config.js
await expect(listRules(targets)).resolves.toContainEqual({
id: '@nx/dependency-checks',
meta: expect.any(Object),
options: expect.any(Array),
} satisfies RuleData);
});
});
});