/
config-autoload.test.js
120 lines (104 loc) · 3.96 KB
/
config-autoload.test.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
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
import path from "path";
import fs from "fs";
import { loadConfig } from "../src/utils";
const testDirectory = path.resolve(__dirname, "fixtures", "config-autoload");
const loaderContext = {
fs,
addBuildDependency: () => true,
addDependency: () => true,
};
describe("autoload config", () => {
it('should load ".postcssrc"', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "rc")
);
expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual("./test/rc/fixtures/index.css");
expect(loadedConfig.config.to).toEqual("./test/rc/expect/index.css");
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(2);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "rc", ".postcssrc")
);
});
it('should load ".postcssrc.js"', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "rc-js")
);
expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual("./test/rc-js/fixtures/index.css");
expect(loadedConfig.config.to).toEqual("./test/rc-js/expect/index.css");
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(2);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "rc-js", ".postcssrc.js")
);
});
it('should load "package.json"', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "pkg")
);
expect(loadedConfig.config.parser).toEqual(false);
expect(loadedConfig.config.syntax).toEqual(false);
expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual("./index.css");
expect(loadedConfig.config.to).toEqual("./index.css");
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(2);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "pkg", "package.json")
);
});
it('should load "postcss.config.js" with "Object" syntax of plugins', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "js/object")
);
expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual(
"./test/fixtures/config-autoload/js/object/index.css"
);
expect(loadedConfig.config.to).toEqual(
"./test/fixtures/config-autoload/js/object/expect/index.css"
);
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(2);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "js/object", "postcss.config.js")
);
});
it('should load "postcss.config.js" with "Array" syntax of plugins', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "js/array")
);
expect(loadedConfig.config.map).toEqual(false);
expect(loadedConfig.config.from).toEqual(
"./test/fixtures/config-autoload/js/object/index.css"
);
expect(loadedConfig.config.to).toEqual(
"./test/fixtures/config-autoload/js/object/expect/index.css"
);
expect(Object.keys(loadedConfig.config.plugins).length).toEqual(4);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "js/array", "postcss.config.js")
);
});
it('should load empty ".postcssrc"', async () => {
const loadedConfig = await loadConfig(
loaderContext,
path.resolve(testDirectory, "empty/.postcssrc")
);
// eslint-disable-next-line no-undefined
expect(loadedConfig.config).toEqual(undefined);
expect(loadedConfig.filepath).toEqual(
path.resolve(testDirectory, "empty/.postcssrc")
);
});
it('should throw an error on "unresolved" config', async () => {
try {
await loadConfig(loaderContext, path.resolve("unresolved"));
} catch (error) {
expect(error.message).toMatch(/^No PostCSS config found in: (.*)$/);
}
});
});