-
Notifications
You must be signed in to change notification settings - Fork 31
/
jest-setup.js
80 lines (66 loc) · 1.96 KB
/
jest-setup.js
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
const fs = require('fs');
const path = require('path');
const plugin = require('.');
const postcss = require('postcss');
const cssInJS = require('postcss-styled-syntax'); // eslint-disable-line import/no-extraneous-dependencies
const html = require('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(plugin(group.options))
.process(item.fixture, { from: undefined })
.then((root) => {
expect(root.css).toEqual(item.expected);
}),
);
});
});
};
global.runTest = function runTest(input, opts, dirname) {
const dir = path.join(dirname, './fixtures/');
const inputSplitted = input.split('.');
let inputName = input;
let inputExt = 'css';
if (inputSplitted.length > 1) {
[inputName, inputExt] = inputSplitted;
}
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 (error) {
fs.writeFileSync(inputPath, inputCSS);
}
try {
expectCSS = fs.readFileSync(expectPath, 'utf8');
} catch (error) {
fs.writeFileSync(expectPath, expectCSS);
}
let syntax;
if (inputExt === 'js') {
syntax = cssInJS;
}
if (inputExt === 'html') {
syntax = html;
}
return postcss([plugin(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);
});
};