Skip to content

Commit 5ea295d

Browse files
authored
tests: promisify the in-memory compiler and remove redundant tests (#27)
1 parent 9328ddc commit 5ea295d

File tree

3 files changed

+47
-85
lines changed

3 files changed

+47
-85
lines changed

lib/run-compilation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var linter = require('./linter');
88
* errors (and their source file locations)
99
* @param options - from the apply method, the options passed in
1010
* @param compilation - the compiler object
11-
* @param done - callback
11+
* @param done - webpack callback
1212
*/
1313
module.exports = function runCompilation(options, compilation, done) {
1414
var errors = [];

test/helpers/pack.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
var webpack = require('webpack');
4+
var MemoryFileSystem = require('memory-fs');
5+
6+
var outputFileSystem = new MemoryFileSystem();
7+
8+
/**
9+
* Basic in memory compiler, promisified
10+
* @param testConfig - the plugin config being tested run through the webpack compiler
11+
* @return {Promise} - rejects with both stats and the error if needed
12+
*/
13+
module.exports = function pack(testConfig) {
14+
return new Promise(function (resolve, reject) {
15+
var compiler = webpack(testConfig);
16+
compiler.outputFileSystem = outputFileSystem;
17+
compiler.run(function (err, stats) {
18+
if (err) {
19+
return reject({err: err, stats: stats});
20+
}
21+
return resolve(stats);
22+
});
23+
});
24+
};

test/index.js

Lines changed: 22 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
/* eslint no-unused-expressions: 0 */
2-
31
'use strict';
42

53
var assign = require('object-assign');
6-
var webpack = require('webpack');
7-
var MemoryFileSystem = require('memory-fs');
84

9-
var StyleLintPlugin = require(getPath('../index'));
10-
var outputFileSystem = new MemoryFileSystem();
5+
var StyleLintPlugin = require('../');
6+
var pack = require('./helpers/pack');
117

128
var configFilePath = getPath('./.stylelintrc');
139
var baseConfig = {
@@ -23,48 +19,33 @@ var baseConfig = {
2319
]
2420
};
2521

26-
// This is the basic in-memory compiler
27-
function pack(testConfig, callback) {
28-
var compiler = webpack(testConfig);
29-
compiler.outputFileSystem = outputFileSystem;
30-
compiler.run(callback);
31-
}
32-
3322
describe('stylelint-webpack-plugin', function () {
34-
it('works with a simple file', function (done) {
23+
it('works with a simple file', function () {
3524
var config = {
3625
context: './test/fixtures/test1',
3726
entry: './index'
3827
};
3928

40-
pack(assign({}, baseConfig, config), function (err, stats) {
41-
expect(err).to.not.exist;
42-
expect(stats.compilation.errors).to.have.length(0);
43-
expect(stats.compilation.warnings).to.have.length(0);
44-
done(err);
45-
});
29+
return pack(assign({}, baseConfig, config))
30+
.then(function (stats) {
31+
expect(stats.compilation.errors).to.have.length(0);
32+
expect(stats.compilation.warnings).to.have.length(0);
33+
});
4634
});
4735

48-
it('sends errors properly', function (done) {
36+
it('sends errors properly', function () {
4937
var config = {
5038
context: './test/fixtures/test3',
51-
entry: './index',
52-
plugins: [
53-
new StyleLintPlugin({
54-
quiet: true,
55-
configFile: configFilePath
56-
})
57-
]
39+
entry: './index'
5840
};
5941

60-
pack(assign({}, baseConfig, config), function (err, stats) {
61-
expect(err).to.not.exist;
62-
expect(stats.compilation.errors).to.have.length(1);
63-
done(err);
64-
});
42+
return pack(assign({}, baseConfig, config))
43+
.then(function (stats) {
44+
expect(stats.compilation.errors).to.have.length(1);
45+
});
6546
});
6647

67-
it('fails on errors', function () {
48+
it('fails on errors when asked to', function () {
6849
var config = {
6950
context: './test/fixtures/test3',
7051
entry: './index',
@@ -77,62 +58,19 @@ describe('stylelint-webpack-plugin', function () {
7758
]
7859
};
7960

80-
return expect(new Promise(function (resolve, reject) {
81-
var compiler = webpack(assign({}, baseConfig, config));
82-
compiler.outputFileSystem = outputFileSystem;
83-
compiler.run(function (err) {
84-
reject(err);
85-
});
86-
})).to.eventually.be.rejectedWith('Error: Failed because of a stylelint error.\n');
87-
});
88-
89-
it('can specify a JSON config file via config', function (done) {
90-
var config = {
91-
context: './test/fixtures/test5',
92-
entry: './index',
93-
plugins: [
94-
new StyleLintPlugin({
95-
configFile: configFilePath,
96-
quiet: true
97-
})
98-
]
99-
};
100-
101-
pack(assign({}, baseConfig, config), function (err, stats) {
102-
expect(err).to.not.exist;
103-
expect(stats.compilation.errors).to.have.length(0);
104-
done(err);
105-
});
61+
expect(pack(assign({}, baseConfig, config)))
62+
.to.eventually.be.rejectedWith('Error: Failed because of a stylelint error.\n');
10663
});
10764

108-
it('works with multiple source files', function (done) {
65+
it('works with multiple source files', function () {
10966
var config = {
11067
context: './test/fixtures/test7',
11168
entry: './index'
11269
};
11370

114-
pack(assign({}, baseConfig, config), function (err, stats) {
115-
expect(err).to.not.exist;
116-
expect(stats.compilation.errors).to.have.length(2);
117-
done(err);
118-
});
71+
return pack(assign({}, baseConfig, config))
72+
.then(function (stats) {
73+
expect(stats.compilation.errors).to.have.length(2);
74+
});
11975
});
120-
121-
// it('should work with multiple context', function(done) {
122-
// var config = {
123-
// context: './test/fixtures/test5',
124-
// entry: './index',
125-
// plugins: [ new StyleLintPlugin({
126-
// configFile: configFilePath,
127-
// context: ['./test/testFiles/test5', './test/testFiles/test7']
128-
// })]
129-
// };
130-
131-
// pack(assign({}, baseConfig, config), function (err, stats) {
132-
// expect(err).to.not.exist;
133-
// expect(stats.compilation.errors.length).to.equal(0);
134-
// expect(stats.compilation.warnings.length).not.to.equal(0);
135-
// done(err);
136-
// });
137-
// });
13876
});

0 commit comments

Comments
 (0)