-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
standalone.test.js
436 lines (372 loc) · 11.2 KB
/
standalone.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"use strict";
const configBlockNoEmpty = require("./fixtures/config-block-no-empty");
const path = require("path");
const standalone = require("../standalone");
const fixturesPath = path.join(__dirname, "fixtures");
describe("standalone with one input file", () => {
let output;
let results;
beforeEach(() => {
return standalone({
files: `${fixturesPath}/empty-block.css`,
// Path to config file
configFile: path.join(__dirname, "fixtures/config-block-no-empty.json")
}).then(data => {
output = data.output;
results = data.results;
});
});
it("triggers warning", () => {
expect(output.indexOf("block-no-empty")).not.toBe(-1);
expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].rule).toBe("block-no-empty");
});
});
describe("standalone with two file-specific globs", () => {
const twoCsses = [
`${fixturesPath}/e*y-block.*`,
`${fixturesPath}/invalid-h*.css`
];
let output;
let results;
beforeEach(() => {
return standalone({
files: twoCsses,
config: {
rules: { "block-no-empty": true, "color-no-invalid-hex": true }
}
}).then(data => {
output = data.output;
results = data.results;
});
});
it("triggers warnings", () => {
expect(output.indexOf("block-no-empty")).not.toBe(-1);
expect(output.indexOf("color-no-invalid-hex")).not.toBe(-1);
expect(results).toHaveLength(2);
expect(results[0].warnings).toHaveLength(1);
expect(results[1].warnings).toHaveLength(1);
// Ordering of the files is non-deterministic, I believe
if (results[0].source.indexOf("empty-block") !== -1) {
expect(results[0].warnings[0].rule).toBe("block-no-empty");
expect(results[1].warnings[0].rule).toBe("color-no-invalid-hex");
} else {
expect(results[1].warnings[0].rule).toBe("block-no-empty");
expect(results[0].warnings[0].rule).toBe("color-no-invalid-hex");
}
});
});
describe("standalone with files and globbyOptions", () => {
let output;
let results;
beforeEach(() => {
return standalone({
files: "empty-block.css",
globbyOptions: { cwd: fixturesPath },
// Path to config file
configFile: path.join(__dirname, "fixtures/config-block-no-empty.json")
}).then(data => {
output = data.output;
results = data.results;
});
});
it("triggers warning", () => {
expect(output.indexOf("block-no-empty")).not.toBe(-1);
expect(results).toHaveLength(1);
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].rule).toBe("block-no-empty");
});
});
it("standalone with input css", () => {
return standalone({
code: "a {}",
config: configBlockNoEmpty
}).then(linted => {
expect(typeof linted.output).toBe("string");
expect(linted.results).toHaveLength(1);
expect(linted.results[0].warnings).toHaveLength(1);
expect(linted.results[0].warnings[0].rule).toBe("block-no-empty");
});
});
it("standalone without input css and file(s) should throw error", () => {
const expectedError = new Error(
"You must pass stylelint a `files` glob or a `code` string, though not both"
);
expect(() => standalone({ config: configBlockNoEmpty })).toThrow(
expectedError
);
});
it("standalone with non-existent-file quietly exits", () => {
return standalone({
files: `${fixturesPath}/non-existent-file.css`,
config: configBlockNoEmpty
}).then(linted => {
expect(typeof linted.output).toBe("string");
expect(linted.results).toHaveLength(0);
expect(linted.errored).toBe(false);
expect(linted.output).toBe("[]");
});
});
describe("standalone passing code with syntax error", () => {
let results;
beforeEach(() => {
return standalone({
code: "a { color: 'red; }",
config: { rules: { "block-no-empty": true } }
}).then(data => (results = data.results));
});
it("<input css 1> as source", () => {
expect(results[0].source).toBe("<input css 1>");
});
it("empty deprecations", () => {
expect(results[0].deprecations).toHaveLength(0);
});
it("empty invalidOptionWarnings", () => {
expect(results[0].invalidOptionWarnings).toHaveLength(0);
});
it("empty parseError", () => {
expect(results[0].parseErrors).toHaveLength(0);
});
it("error registered", () => {
expect(results[0].errored).toBeTruthy();
});
it("syntax error rule is CssSyntaxError", () => {
expect(results[0].warnings).toHaveLength(1);
expect(results[0].warnings[0].rule).toBe("CssSyntaxError");
});
it("syntax error severity is error", () => {
expect(results[0].warnings[0].severity).toBe("error");
});
it("(CssSyntaxError) in warning text", () => {
expect(results[0].warnings[0].text.indexOf(" (CssSyntaxError)")).not.toBe(
-1
);
});
});
it("standalone passing file with syntax error", () => {
return standalone({
code: "a { color: 'red; }",
codeFilename: path.join(__dirname, "syntax-error.css"),
config: { rules: { "block-no-empty": true } }
}).then(linted => {
expect(linted.results[0].source.indexOf("syntax-error.css")).not.toBe(-1);
});
});
it("syntax error sets errored to true", () => {
return standalone({
code: "a { color: 'red; }",
config: { rules: { "block-no-empty": true } }
}).then(linted => {
expect(linted.errored).toBe(true);
});
});
it("error `Cannot parse selector` sets errored to true", () => {
return standalone({
code: "data-something='true'] { }",
config: { rules: { "selector-type-no-unknown": true } }
}).then(linted => {
expect(linted.errored).toBe(true);
});
});
it("configuration error sets errored to true", () => {
return standalone({
code: "a { color: 'red'; }",
config: { rules: { "block-no-empty": "wahoo" } }
}).then(linted => {
expect(linted.errored).toBe(true);
});
});
it("unknown syntax option", () => {
return standalone({
syntax: "unknown",
code: "",
config: { rules: { "block-no-empty": "wahoo" } }
})
.then(() => {
throw new Error("should not have succeeded");
})
.catch(err => {
expect(err.message).toBe(
"You must use a valid syntax option, either: css-in-js, html, less, markdown, sass, scss or sugarss"
);
});
});
it("unknown custom syntax option", () => {
return standalone({
customSyntax: "unknown-module",
code: "",
config: { rules: { "block-no-empty": "wahoo" } }
})
.then(() => {
throw new Error("should not have succeeded");
})
.catch(err => {
expect(err.message).toBe(
"Cannot resolve custom syntax module unknown-module"
);
});
});
it("unknown formatter option", () => {
return standalone({
formatter: "unknown",
code: "",
config: { rules: { "block-no-empty": "wahoo" } }
})
.then(() => {
throw new Error("should not have succeeded");
})
.catch(err => {
expect(err.message.indexOf("You must use a valid formatter option")).toBe(
0
);
});
});
describe("standalone with different configs per file", () => {
let results;
beforeEach(() => {
return standalone({
files: `${fixturesPath}/config-per-file/**/*.css`
}).then(data => (results = data.results));
});
it("no warnings for A", () => {
const resultA = results.find(
result => result.source.indexOf("a.css") !== -1
);
expect(resultA.warnings).toHaveLength(0);
});
it("one warning for B", () => {
const resultB = results.find(
result => result.source.indexOf("b.css") !== -1
);
expect(resultB.warnings).toHaveLength(1);
});
it("correct warning for B", () => {
const resultB = results.find(
result => result.source.indexOf("b.css") !== -1
);
expect(resultB.warnings[0].text.indexOf("Unexpected empty block")).not.toBe(
-1
);
});
it("no warnings for C", () => {
const resultC = results.find(
result => result.source.indexOf("c.css") !== -1
);
expect(resultC.warnings).toHaveLength(0);
});
it("no warnings for D", () => {
const resultD = results.find(
result => result.source.indexOf("d.css") !== -1
);
expect(resultD.warnings).toHaveLength(0);
});
});
describe("standalone with config locatable from process.cwd not file", () => {
let actualCwd;
let results;
beforeAll(() => {
actualCwd = process.cwd();
process.chdir(path.join(__dirname, "./fixtures/getConfigForFile/a/b"));
});
afterAll(() => {
process.chdir(actualCwd);
});
beforeEach(() => {
return standalone({
files: [path.join(__dirname, "./fixtures/empty-block.css")]
}).then(data => (results = data.results));
});
it("two warning", () => {
expect(results[0].warnings).toHaveLength(2);
});
it("'block-no-empty' correct warning", () => {
expect(
results[0].warnings.find(warn => warn.rule === "block-no-empty")
).toBeTruthy();
});
it("'plugin/warn-about-foo' correct warning", () => {
expect(
results[0].warnings.find(warn => warn.rule === "plugin/warn-about-foo")
).toBeTruthy();
});
});
describe("configOverrides", () => {
it("Setting `plugins` inside `**/__tests__/**` object should override the ones set in `config` object", () => {
return standalone({
code: ".bar {}",
configBasedir: __dirname,
config: {
plugins: [],
rules: {
"plugin/warn-about-bar": "always"
}
},
configOverrides: {
plugins: ["./fixtures/plugin-warn-about-bar"]
}
}).then(linted => {
expect(linted.results[0].warnings).toHaveLength(1);
expect(linted.results[0].warnings[0].text).toBe(
"found .bar (plugin/warn-about-bar)"
);
});
});
it("Setting `extends` inside `configOverrides` object should override the ones set in `config` object", () => {
return standalone({
code: ".bar {}",
configBasedir: __dirname,
config: {
extends: ["foo"]
},
configOverrides: {
extends: ["./fixtures/config-block-no-empty"]
}
}).then(linted => {
expect(linted.results[0].warnings).toHaveLength(1);
expect(linted.results[0].warnings[0].text).toContain("block-no-empty");
});
});
});
describe("nonexistent codeFilename with loaded config", () => {
let actualCwd;
beforeAll(() => {
actualCwd = process.cwd();
process.chdir(path.join(__dirname, "./fixtures/getConfigForFile/a/b"));
});
afterAll(() => {
process.chdir(actualCwd);
});
it("does not cause error", () => {
return standalone({
code: "a {}",
codeFilename: "does-not-exist.css"
});
});
it("does load config from process.cwd", () => {
return standalone({
code: "a {}",
codeFilename: "does-not-exist.css"
}).then(linted => {
expect(linted.results[0].warnings).toHaveLength(1);
});
});
});
describe("existing codeFilename for nested config detection", () => {
let actualCwd;
beforeAll(() => {
actualCwd = process.cwd();
process.chdir(path.join(__dirname, "./fixtures/getConfigForFile"));
});
afterAll(() => {
process.chdir(actualCwd);
});
it("loads config from a nested directory", () => {
return standalone({
code: "a {}",
codeFilename: "a/b/foo.css"
}).then(linted => {
expect(linted.results[0].warnings).toHaveLength(1);
});
});
});