Skip to content

Commit b17a4a7

Browse files
authored
fix: quiet flag only prints when set to false (#60)
* refactor: pull out errorMessage constant
1 parent e2f33b8 commit b17a4a7

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ function apply (options, compiler) {
1515
options = options || {};
1616
var context = options.context || compiler.context;
1717
options = assign({
18-
formatter: formatter,
19-
quiet: false
18+
formatter: formatter
2019
}, options, {
2120
// Default Glob is any directory level of scss and/or sass file,
2221
// under webpack's context and specificity changed via globbing patterns

lib/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

33
module.exports = {
4-
defaultFilesGlob: '**/*.s?(c|a)ss'
4+
defaultFilesGlob: '**/*.s?(c|a)ss',
5+
errorMessage: 'Failed because of a stylelint error.\n'
56
};

lib/run-compilation.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var chalk = require('chalk');
44
var linter = require('./linter');
5+
var errorMessage = require('./constants').errorMessage;
56

67
/**
78
* Function bound to the plugin `apply` method to run the linter and report any
@@ -26,19 +27,19 @@ module.exports = function runCompilation (options, compiler, done) {
2627
return file.errored;
2728
});
2829

29-
if (!options.quiet) {
30-
console.log(chalk.yellow(options.formatter(results)));
30+
if (options.quiet === false) {
31+
console.warn(options.formatter(results));
3132
}
3233

3334
if (options.failOnError && errors.length) {
34-
done(new Error('Failed because of a stylelint error.\n'));
35+
done(new Error(errorMessage));
3536
} else {
3637
done();
3738
}
3839
})
3940
.catch(done);
4041

41-
compiler.plugin('after-emit', function onCompilation (compilation, callback) {
42+
compiler.plugin('after-emit', function afterEmit (compilation, callback) {
4243
if (warnings.length) {
4344
compilation.warnings.push(chalk.yellow(options.formatter(warnings)));
4445
warnings = [];

test/index.test.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict';
22

33
var assign = require('object-assign');
4+
var td = require('testdouble');
45
var StyleLintPlugin = require('../');
56
var pack = require('./helpers/pack');
67
var webpack = require('./helpers/webpack');
78
var baseConfig = require('./helpers/base-config');
89

910
var configFilePath = getPath('./.stylelintrc');
11+
var errorMessage = require('../lib/constants').errorMessage;
1012

1113
describe('stylelint-webpack-plugin', function () {
1214
it('works with a simple file', function () {
@@ -51,7 +53,7 @@ describe('stylelint-webpack-plugin', function () {
5153
return pack(assign({}, baseConfig, config))
5254
.then(expect.fail)
5355
.catch(function (err) {
54-
expect(err.message).to.equal('Failed because of a stylelint error.\n');
56+
expect(err.message).to.equal(errorMessage);
5557
});
5658
});
5759

@@ -82,26 +84,6 @@ describe('stylelint-webpack-plugin', function () {
8284
});
8385
});
8486

85-
// TODO use snapshots to ensure something is printed to the console
86-
it.skip('sends messages to console when quiet prop set to false', function () {
87-
var config = {
88-
context: './test/fixtures/syntax-error',
89-
entry: './index',
90-
plugins: [
91-
new StyleLintPlugin({
92-
configFile: configFilePath,
93-
quiet: true
94-
})
95-
]
96-
};
97-
98-
return pack(assign({}, baseConfig, config))
99-
.then(function (stats) {
100-
expect(stats.compilation.errors).to.have.length(1);
101-
expect(stats.compilation.warnings).to.have.length(0);
102-
});
103-
});
104-
10587
it('fails when .stylelintrc is not a proper format', function () {
10688
var config = {
10789
entry: './index',
@@ -121,6 +103,35 @@ describe('stylelint-webpack-plugin', function () {
121103
});
122104
});
123105

106+
context('iff quiet is strictly false', function () {
107+
beforeEach(function () {
108+
td.replace(console, 'warn', td.function());
109+
});
110+
111+
afterEach(function () {
112+
td.reset();
113+
});
114+
115+
it('sends messages to the console', function () {
116+
var config = {
117+
context: './test/fixtures/syntax-error',
118+
entry: './index',
119+
plugins: [
120+
new StyleLintPlugin({
121+
configFile: configFilePath,
122+
quiet: false
123+
})
124+
]
125+
};
126+
127+
return pack(assign({}, baseConfig, config))
128+
.then(function (stats) {
129+
expect(stats.compilation.errors).to.have.length(1);
130+
td.verify(console.warn(td.matchers.contains('✖')));
131+
});
132+
});
133+
});
134+
124135
context('without StyleLintPlugin configuration', function () {
125136
var config = {
126137
context: './test/fixtures/lint-free',

0 commit comments

Comments
 (0)