forked from stylelint/stylelint
-
Notifications
You must be signed in to change notification settings - Fork 2
/
standalone-formatter.test.js
102 lines (86 loc) · 2.87 KB
/
standalone-formatter.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
'use strict';
const configBlockNoEmpty = require('./fixtures/config-block-no-empty');
const standalone = require('../standalone');
const stringFormatter = require('../formatters/stringFormatter');
const stripAnsi = require('strip-ansi');
it('standalone with input css and alternate formatter specified by keyword', () => {
return standalone({
code: 'a {}',
config: configBlockNoEmpty,
formatter: 'string',
}).then((linted) => {
const output = linted.output;
const strippedOutput = stripAnsi(output);
expect(typeof output).toBe('string');
expect(strippedOutput.indexOf('1:3')).not.toBe(-1);
expect(strippedOutput.indexOf('block-no-empty')).not.toBe(-1);
});
});
it('standalone with input css and alternate formatter function', () => {
return standalone({
code: 'a {}',
config: configBlockNoEmpty,
formatter: stringFormatter,
}).then((linted) => {
const output = linted.output;
const strippedOutput = stripAnsi(output);
expect(typeof output).toBe('string');
expect(strippedOutput.indexOf('1:3')).not.toBe(-1);
expect(strippedOutput.indexOf('block-no-empty')).not.toBe(-1);
});
});
it('standalone with invalid formatter option', () => {
return standalone({
code: 'a {}',
config: configBlockNoEmpty,
formatter: 'invalid',
}).catch((err) => {
expect(err).toEqual(
new Error(
'You must use a valid formatter option: "compact", "json", "string", "unix", "verbose" or a function',
),
);
});
});
it('standalone formatter receives {needlessDisables} as second argument', () => {
const formatter = jest.fn((results, { needlessDisables }) => {
return JSON.stringify({ results, needlessDisables }, null, 2);
});
return standalone({
code: `
/* stylelint-disable yo */
a {}
`,
config: configBlockNoEmpty,
reportNeedlessDisables: true,
formatter,
}).then((linted) => {
const { output, results, needlessDisables } = linted;
expect(needlessDisables).not.toBeUndefined();
expect(needlessDisables).toHaveLength(1);
expect(typeof output).toBe('string');
expect(formatter).toHaveBeenCalledTimes(1);
expect(output).toBe(JSON.stringify({ results, needlessDisables }, null, 2));
});
});
it('standalone formatter receives {invalidScopeDisables} as second argument', () => {
const formatter = jest.fn((results, { invalidScopeDisables }) => {
return JSON.stringify({ results, invalidScopeDisables }, null, 2);
});
return standalone({
code: `
/* stylelint-disable indentation */
a { color: red; }
`,
config: configBlockNoEmpty,
reportInvalidScopeDisables: true,
formatter,
}).then((linted) => {
const { output, results, invalidScopeDisables } = linted;
expect(invalidScopeDisables).not.toBeUndefined();
expect(invalidScopeDisables).toHaveLength(1);
expect(typeof output).toBe('string');
expect(formatter).toHaveBeenCalledTimes(1);
expect(output).toBe(JSON.stringify({ results, invalidScopeDisables }, null, 2));
});
});