Repository navigation
Expand file tree
/
Copy pathjest-setup.js
More file actions
75 lines (62 loc) · 1.88 KB
/
Copy pathjest-setup.js
File metadata and controls
75 lines (62 loc) · 1.88 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
import fs from 'node:fs';
import path from 'node:path';
import postcssSorting from './index.js';
import postcss from 'postcss';
import cssInJS from 'postcss-styled-syntax';
import html from 'postcss-html'; // eslint-disable-line import/no-extraneous-dependencies
global.groupTest = function groupTest(testGroups) {
testGroups.forEach((group) => {
group.cases.forEach((item) => {
const message =
item.description ||
group.message ||
`Should work with ${JSON.stringify(group.options)}`;
const testFn = item.only ? test.only : test;
testFn(message, () =>
postcss(postcssSorting(group.options))
.process(item.fixture, { from: undefined })
.then((root) => {
expect(root.css).toEqual(item.expected);
}),
);
});
});
};
global.runTest = function runTest(input, opts) {
const testPath = /** @type {string} */ (expect.getState().testPath);
const dir = path.join(path.dirname(testPath), './fixtures/');
const [inputName = input, inputExt = 'css'] = input.split('.');
const inputPath = path.resolve(`${dir + inputName}.${inputExt}`);
const expectPath = path.resolve(`${dir + inputName}.expected.${inputExt}`);
const actualPath = path.resolve(`${dir + inputName}.actual.${inputExt}`);
let inputCSS = '';
let expectCSS = '';
try {
inputCSS = fs.readFileSync(inputPath, 'utf8');
} catch {
fs.writeFileSync(inputPath, inputCSS);
}
try {
expectCSS = fs.readFileSync(expectPath, 'utf8');
} catch {
fs.writeFileSync(expectPath, expectCSS);
}
let syntax;
if (inputExt === 'js') {
syntax = cssInJS;
}
if (inputExt === 'html') {
syntax = html;
}
return postcss([postcssSorting(opts)])
.process(inputCSS, {
from: inputPath,
syntax,
})
.then((result) => {
const actualCSS = result.css;
fs.writeFileSync(actualPath, actualCSS);
expect(result.css).toEqual(expectCSS);
expect(result.warnings().length).toEqual(0);
});
};