/
index.test.js
266 lines (235 loc) · 8.69 KB
/
index.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
'use strict';
const assign = require('object-assign');
const path = require('path');
const td = require('testdouble');
const StyleLintPlugin = require('../');
const pack = require('./helpers/pack');
const webpack = require('./helpers/webpack');
const baseConfig = require('./helpers/base-config');
const configFilePath = getPath('./.stylelintrc');
const errorMessage = require('../lib/constants').errorMessage;
describe('stylelint-webpack-plugin', function () {
it('works with a simple file', function () {
return pack(assign({}, baseConfig, { context: path.resolve('./test/fixtures/lint-free') }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(0);
});
});
it('sends errors to the errors output only', function () {
return pack(assign({}, baseConfig, { context: path.resolve('./test/fixtures/single-error') }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(1, 'should have one error');
expect(stats.compilation.warnings).to.have.length(0, 'should have no warnings');
});
});
it('works with multiple source files', function () {
return pack(assign({}, baseConfig, { context: path.resolve('./test/fixtures/multiple-sources') }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(1);
expect(stats.compilation.errors[0]).to.be.an.instanceof(Error);
expect(stats.compilation.errors[0].message).to.contain('test/fixtures/multiple-sources/_second.scss');
expect(stats.compilation.errors[0].message).to.contain('test/fixtures/multiple-sources/test.scss');
});
});
it('sends warnings properly', function () {
return pack(assign({}, baseConfig, { context: path.resolve('./test/fixtures/rule-warning') }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(1);
});
});
it('fails on errors when asked to', function () {
const config = {
context: path.resolve('./test/fixtures/single-error'),
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true,
failOnError: true
})
]
};
return pack(assign({}, baseConfig, config))
.then(expect.fail)
.catch(function (err) {
expect(err.message).to.equal(errorMessage);
});
});
it('fails when .stylelintrc is not a proper format', function () {
const config = {
context: path.resolve('./test/fixtures/single-error'),
plugins: [
new StyleLintPlugin({
configFile: getPath('./.badstylelintrc'),
quiet: true
})
]
};
return pack(assign({}, baseConfig, config))
.then(expect.fail)
.catch(function (err) {
expect(err.message).to.contain('Failed to parse').and.contain('as JSON');
});
});
context('iff quiet is strictly false', function () {
beforeEach(function () {
td.replace(console, 'warn', td.function());
});
afterEach(function () {
td.reset();
});
it('sends messages to the console', function () {
const config = {
context: path.resolve('./test/fixtures/syntax-error'),
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: false
})
]
};
return pack(assign({}, baseConfig, config))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(1);
td.verify(console.warn(td.matchers.contains('✖')));
});
});
});
context('without StyleLintPlugin configuration', function () {
const config = {
plugins: [
new StyleLintPlugin()
]
};
it('works by using stylelint#cosmiconfig under the hood', function () {
return pack(assign({}, baseConfig, config, { context: path.resolve('./test/fixtures/lint-free') }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(0);
});
});
it('finds the right stylelintrc', function () {
return pack(assign({}, baseConfig, config, { context: path.resolve('./test/fixtures/rule-warning') }))
.then(function (stats) {
expect(stats.compilation.warnings).to.have.length(1);
});
});
});
context('interop with NoErrorsPlugin', function () {
it('works when failOnError is false', function () {
const config = {
context: path.resolve('./test/fixtures/single-error'),
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true
}),
new webpack.NoErrorsPlugin()
]
};
return pack(assign({}, baseConfig, config))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(1);
});
});
context('when failOnError is true', function () {
const config = {
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true,
failOnError: true
}),
new webpack.NoErrorsPlugin()
]
};
it('throws when there is an error', function () {
return pack(assign({}, baseConfig, config, { context: path.resolve('./test/fixtures/single-error') }))
.then(expect.fail)
.catch(function (err) {
expect(err).to.be.instanceof(Error);
});
});
it('does not throw when there are only warnings', function () {
return pack(assign({}, baseConfig, config, { context: path.resolve('./test/fixtures/rule-warning') }))
.then(function (stats) {
expect(stats.compilation.warnings).to.have.length(1);
});
});
});
});
context('when `emitErrors` is disabled', function () {
const config = {
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true,
emitErrors: false
})
]
};
it('does not print warnings or errors when there are none', function () {
return pack(assign({}, baseConfig, config, { context: path.resolve('./test/fixtures/lint-free') }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(0);
});
});
it('emits errors as warnings when asked to', function () {
return pack(assign({}, baseConfig, config, { context: path.resolve('./test/fixtures/single-error') }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(1);
expect(stats.compilation.warnings[0]).to.be.an.instanceof(Error);
expect(stats.compilation.warnings[0].message).to.contain('✖');
});
});
it('still indicates that warnings are warnings, even when emitting errors as warnings too', function () {
return pack(assign({}, baseConfig, config, { context: path.resolve('./test/fixtures/rule-warning') }))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(1);
expect(stats.compilation.warnings[0]).to.be.an.instanceof(Error);
expect(stats.compilation.warnings[0].message).to.contain('⚠');
});
});
});
context('lintDirtyModulesOnly flag is enabled', function () {
it('skips linting on initial run', function () {
const config = {
context: path.resolve('./test/fixtures/single-error'),
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true,
lintDirtyModulesOnly: true
})
]
};
return pack(assign({}, baseConfig, config))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(0);
});
});
it('still skips on initial run with `emitErrors` disabled', function () {
const config = {
context: path.resolve('./test/fixtures/single-error'),
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true,
lintDirtyModulesOnly: true,
emitErrors: false
})
]
};
return pack(assign({}, baseConfig, config))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(0);
});
});
});
});