-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathstylelint-prettier-e2e.test.js
More file actions
149 lines (121 loc) · 4.68 KB
/
stylelint-prettier-e2e.test.js
File metadata and controls
149 lines (121 loc) · 4.68 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
import {describe, test} from 'node:test';
import assert from 'node:assert/strict';
import {spawnSync} from 'node:child_process';
import {resolve, relative, dirname} from 'node:path';
import {fileURLToPath} from 'node:url';
import stylelint from 'stylelint';
import baseConfig from './fixtures/stylelint.config.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const stylelintCwd = `${__dirname}/fixtures`;
/**
* Tests that report errors in multiple files may change the order of the files
* across multiple runs.
* To avoid flaky tests, assert the reporting of errors in one file only per
* test case. Asserting no errors are reported across multiple files is ok.
*/
describe('E2E Tests', () => {
test('CSS files', () => {
const result = runStylelint('*.css');
const expectedResult = `
::error file=check.invalid.css,line=2,col=25,endLine=2,endColumn=28,title=Stylelint problem::Replace ""x"" with "'x'" (prettier/prettier)
`.trim();
assert.strictEqual(result.output, '');
assert.strictEqual(result.error, expectedResult);
assert.strictEqual(result.status, 2);
});
test('SCSS files', () => {
const result = runStylelint('*.scss');
const expectedResult = `
::error file=check.invalid.scss,line=2,col=25,endLine=2,endColumn=28,title=Stylelint problem::Replace ""x"" with "'x'" (prettier/prettier)
::error file=check.invalid.scss,line=8,col=14,endLine=8,endColumn=15,title=Stylelint problem::Insert "," (prettier/prettier)
`.trim();
assert.strictEqual(result.output, '');
assert.strictEqual(result.error, expectedResult);
assert.strictEqual(result.status, 2);
});
test('LESS files', () => {
const result = runStylelint('*.less');
const expectedResult = `
::error file=check.invalid.less,line=2,col=25,endLine=2,endColumn=28,title=Stylelint problem::Replace ""x"" with "'x'" (prettier/prettier)
::error file=check.invalid.less,line=8,col=12,endLine=8,endColumn=13,title=Stylelint problem::Insert ";" (prettier/prettier)
::error file=check.invalid.less,line=12,col=1,endLine=12,endColumn=2,title=Stylelint problem::Insert "··" (prettier/prettier)
`.trim();
assert.strictEqual(result.output, '');
assert.strictEqual(result.error, expectedResult);
assert.strictEqual(result.status, 2);
});
/**
* Don't act upon html-like files, as prettier already handles them as whole
* files
*/
test('HTML/Markdown/Vue/Svelte/Astro files', () => {
const result = runStylelint('*.{html,md,vue,svelte,astro}');
const expectedResult = ``;
assert.strictEqual(result.output, '');
assert.strictEqual(result.error, expectedResult);
assert.strictEqual(result.status, 0);
});
/**
* Don't act upon CSS-in-JS files, as prettier already handles them as whole
* files
*/
test('CSS-in-JS files', () => {
const result = runStylelint('*.{js,jsx,tsx}');
const expectedResult = ``;
assert.strictEqual(result.output, '');
assert.strictEqual(result.error, expectedResult);
assert.strictEqual(result.status, 0);
});
/** @see https://github.com/prettier/stylelint-prettier/issues/354 */
test('the --fix option works correctly with other rules', async () => {
const inputCode = `.a {\n color: #ffffff;\n font-size: 16px\n}\n`;
const fixConfig = structuredClone(baseConfig);
fixConfig.rules['color-hex-length'] = 'short';
const {code: outputCode} = await stylelint.lint({
code: inputCode,
configBasedir: stylelintCwd,
fix: true,
config: fixConfig,
});
assert.strictEqual(
outputCode,
'.a {\n color: #fff;\n font-size: 16px;\n}\n'
);
});
});
function runStylelint(pattern) {
const stylelintCmd = resolve(`${__dirname}/../node_modules/.bin/stylelint`);
// Use json formatter as it is less likely to change across releases
const result = spawnSync(stylelintCmd, ['--formatter=json', pattern], {
cwd: stylelintCwd,
});
const resultContent = result.stderr.toString().trim();
let jsonErrors;
try {
jsonErrors = JSON.parse(resultContent);
} catch (err) {
throw new Error(
`Could not parse json from stderr. Attempted to parse:\n${resultContent}`,
{cause: err}
);
}
const errorLines = [];
for (const error of jsonErrors) {
for (const warning of error.warnings) {
errorLines.push(
`::error file=${relative(stylelintCwd, error.source)}` +
`,line=${warning.line}` +
`,col=${warning.column}` +
`,endLine=${warning.endLine}` +
`,endColumn=${warning.endColumn}` +
`,title=Stylelint problem` +
`::${warning.text}`
);
}
}
return {
status: result.status,
output: result.stdout.toString().trim(),
error: errorLines.join('\n'),
};
}