-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
index.test.mjs
109 lines (83 loc) · 3.5 KB
/
index.test.mjs
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
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { execSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { readFile } from 'node:fs/promises';
import rules from '../index.js';
const require = createRequire(import.meta.url);
const __dirname = fileURLToPath(new URL('.', import.meta.url));
const ruleEntries = Object.entries(rules);
let rulesListDoc;
beforeAll(async () => {
rulesListDoc = await readFile(
path.join(__dirname, '..', '..', '..', 'docs', 'user-guide', 'rules.md'),
'utf8',
);
});
describe('all rules', () => {
test.each(ruleEntries)('"%s" should have metadata', (name, rule) => {
expect(rule.meta).toBeTruthy();
expect(rule.meta.url).toBe(`https://stylelint.io/user-guide/rules/${name}`);
expect([true, undefined]).toContain(rule.meta.fixable);
});
test.each(ruleEntries)('"%s" should have a link to a rule doc in the rules page', (name) => {
expect(rulesListDoc).toContain(`[\`${name}\`](../../lib/`);
});
});
describe('fixable rules', () => {
const fixableRules = ruleEntries.filter(([, rule]) => rule.meta.fixable);
test.each(fixableRules)('"%s" should describe fixable in the rules doc', async (name) => {
const doc = await readFile(path.join(__dirname, '..', name, 'README.md'), 'utf8');
expect(doc).toMatch('`fix` option');
});
test.each(fixableRules)('"%s" should describe fixable in the rules doc', (name) => {
expect(rulesListDoc).toMatch(new RegExp(`^.+\`${name}\`.+\\|.+\\|\\s+🔧\\s+\\|$`, 'm'));
});
});
describe('deprecated rules', () => {
const deprecatedRules = ruleEntries.filter(([, rule]) => rule.meta.deprecated);
test.each(deprecatedRules)('"%s" should describe deprecation in the rules doc', async (name) => {
const doc = await readFile(path.join(__dirname, '..', name, 'README.md'), 'utf8');
expect(doc).toMatch('> **Warning**');
});
});
describe('custom message option', () => {
test.each(ruleEntries)(
'"%s" should describe a custom message option in its doc',
async (ruleName) => {
const jsFile = path.join(__dirname, '..', ruleName, 'index.js');
const jsCode = await readFile(jsFile, 'utf8');
// NOTE: If all rules support a custom message option, we should remove this `if` statement.
if (!jsCode.includes('\tmessageArgs: [')) return;
const doc = await readFile(jsFile.replace('index.js', 'README.md'), 'utf8');
expect(doc).toContain('`message` secondary option');
},
);
});
describe('standard config', () => {
const tmpDir = path.join(__dirname, 'tmp');
// NOTE: The use of Promised-based APIs may cause flaky test on CI.
rmSync(tmpDir, { recursive: true, force: true });
mkdirSync(tmpDir, { recursive: true });
writeFileSync(path.join(tmpDir, 'package.json'), '{}');
execSync(
'npm install --silent --no-package-lock --no-audit --omit=peer stylelint-config-standard',
{ cwd: tmpDir },
);
const configRules = (name) => {
const config = require(path.join(tmpDir, 'node_modules', name));
return Object.keys(config.rules);
};
const standardRules = configRules('stylelint-config-standard');
standardRules.push(...configRules('stylelint-config-recommended'));
afterAll(() => {
rmSync(tmpDir, { recursive: true, force: true });
});
test('the rules are not empty', () => {
expect(standardRules).not.toHaveLength(0);
});
test.each(standardRules)('the rule "%s" are present in the rules doc', (name) => {
expect(rulesListDoc).toMatch(new RegExp(`^.+\`${name}\`.+\\|\\s+✅\\s+\\|.+\\|$`, 'm'));
});
});