-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
186 lines (162 loc) · 5.3 KB
/
index.js
File metadata and controls
186 lines (162 loc) · 5.3 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const webpack = require("webpack");
const PrettierPlugin = require("../index.js");
const uuid = require("uuid").v4;
const fs = require("fs");
const sampleCodeFilename = "./__tests__/sample-code.js";
const sampleCode = fs.readFileSync(sampleCodeFilename, { encoding: "utf8" });
const bundle = (config, alternative) => {
return new Promise((resolve, reject) => {
webpack(config, (err, stats) => {
if (err) return reject(err);
const errors = stats.toString("errors-only");
if (errors) return reject(errors);
// If we provided an alternative target, compare it to sample code
const target = alternative ? alternative : config.entry;
fs.readFile(target, { encoding: "utf8" }, (err, code) => {
if (err) return reject(err);
let didFileUpdate = false;
if (code !== sampleCode) didFileUpdate = true;
if (!didFileUpdate) return reject("File did not change!");
resolve(code);
});
});
});
};
const prepareEntryWithExtras = async (code, extras, file) => {
return new Promise((resolve, reject) => {
let fileContents = "";
extras.forEach(extra => {
fileContents += extra + "\n";
});
fileContents += code;
fs.writeFile(file, fileContents, err => {
if (err) reject(err);
else resolve();
});
});
};
const prepareEntry = async (code, file) => {
return new Promise((resolve, reject) => {
fs.writeFile(file, code, err => {
if (err) reject(err);
else resolve();
});
});
};
const teardown = path => {
fs.readdir(path, (err, filenames) => {
filenames.forEach(file => {
const curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) teardown(curPath);
else fs.unlinkSync(curPath);
});
fs.rmdirSync(path);
});
};
describe("unit tests", () => {
beforeAll(() => {
if (!fs.existsSync("./temp")) fs.mkdirSync("./temp");
});
afterAll(() => {
teardown("./temp");
});
it("prettifies source", async () => {
const input = `./temp/${uuid()}.js`;
const output = `./temp/${uuid()}.js`;
await prepareEntry(sampleCode, input);
const processed = await bundle({
entry: input,
output: { filename: output },
plugins: [new PrettierPlugin()]
});
return expect(processed).toMatchSnapshot();
});
it("ignores unexpected config options in case they are for prettier", async () => {
const input = `./temp/${uuid()}.js`;
const output = `./temp/${uuid()}.js`;
await prepareEntry(sampleCode, input);
return bundle({
entry: input,
output: { filename: output },
plugins: [new PrettierPlugin({ maybeForPrettier: true })]
});
});
it("respects prettier config options", async () => {
const input = `./temp/${uuid()}.js`;
const output = `./temp/${uuid()}.js`;
await prepareEntry(sampleCode, input);
let processed = await bundle({
entry: input,
output: { filename: output },
plugins: [new PrettierPlugin({ singleQuote: true })]
});
expect(processed).toMatchSnapshot();
await prepareEntry(sampleCode, input);
processed = await bundle({
entry: input,
output: { filename: output },
plugins: [new PrettierPlugin({ singleQuote: false })]
});
return expect(processed).toMatchSnapshot();
});
it("respects prettier config options from file", async () => {
const input = `./temp/${uuid()}.js`;
const output = `./temp/${uuid()}.js`;
await prepareEntry(sampleCode, input);
let processed = await bundle({
entry: input,
output: { filename: output },
plugins: [new PrettierPlugin({ configFile: `${process.cwd()}/prettier.config.json` })]
});
expect(processed).toMatchSnapshot();
await prepareEntry(sampleCode, input);
processed = await bundle({
entry: input,
output: { filename: output },
plugins: [new PrettierPlugin({ singleQuote: false, configFile: `${process.cwd()}/prettier.config.json` })]
});
return expect(processed).toMatchSnapshot();
});
// TODO: Why does this fail to catch Prettier > `jest-validate` Validation Error?
// it("throws on invalid prettier config options", async () => {
// const input = `./temp/${uuid()}.js`;
// const output = `./temp/${uuid()}.js`;
//
// await prepareEntry(sampleCode, input);
//
// return expect(
// bundle({
// entry: input,
// output: { filename: output },
// plugins: [new PrettierPlugin({ singleQuote: () => null })]
// })
// ).rejects.toMatchSnapshot();
// });
it("only processes files with specified extensions", async () => {
const entry = `./temp/${uuid()}.js`;
const moduleUUID = uuid();
const module = `./temp/${moduleUUID}.jsx`;
const moduleRelativeToEntry = `./${moduleUUID}.jsx`;
const output = `./temp/${uuid()}.js`;
await Promise.all([
prepareEntryWithExtras(
sampleCode,
[`const module = require("${moduleRelativeToEntry}")`],
entry
),
prepareEntry(sampleCode, module)
]);
// Expect the module to not change
return expect(
bundle(
{
entry: entry,
mode: 'development',
output: { filename: output },
plugins: [new PrettierPlugin({ extensions: [".js"] })]
},
module
)
).rejects.toBe("File did not change!");
});
});