Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: promisify the in-memory compiler and remove redundant tests #27

Merged
merged 1 commit into from
Aug 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/run-compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var linter = require('./linter');
* errors (and their source file locations)
* @param options - from the apply method, the options passed in
* @param compilation - the compiler object
* @param done - callback
* @param done - webpack callback
*/
module.exports = function runCompilation(options, compilation, done) {
var errors = [];
Expand Down
24 changes: 24 additions & 0 deletions test/helpers/pack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

var webpack = require('webpack');
var MemoryFileSystem = require('memory-fs');

var outputFileSystem = new MemoryFileSystem();

/**
* Basic in memory compiler, promisified
* @param testConfig - the plugin config being tested run through the webpack compiler
* @return {Promise} - rejects with both stats and the error if needed
*/
module.exports = function pack(testConfig) {
return new Promise(function (resolve, reject) {
var compiler = webpack(testConfig);
compiler.outputFileSystem = outputFileSystem;
compiler.run(function (err, stats) {
if (err) {
return reject({err: err, stats: stats});
}
return resolve(stats);
});
});
};
106 changes: 22 additions & 84 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* eslint no-unused-expressions: 0 */

'use strict';

var assign = require('object-assign');
var webpack = require('webpack');
var MemoryFileSystem = require('memory-fs');

var StyleLintPlugin = require(getPath('../index'));
var outputFileSystem = new MemoryFileSystem();
var StyleLintPlugin = require('../');
var pack = require('./helpers/pack');

var configFilePath = getPath('./.stylelintrc');
var baseConfig = {
Expand All @@ -23,48 +19,33 @@ var baseConfig = {
]
};

// This is the basic in-memory compiler
function pack(testConfig, callback) {
var compiler = webpack(testConfig);
compiler.outputFileSystem = outputFileSystem;
compiler.run(callback);
}

describe('stylelint-webpack-plugin', function () {
it('works with a simple file', function (done) {
it('works with a simple file', function () {
var config = {
context: './test/fixtures/test1',
entry: './index'
};

pack(assign({}, baseConfig, config), function (err, stats) {
expect(err).to.not.exist;
expect(stats.compilation.errors).to.have.length(0);
expect(stats.compilation.warnings).to.have.length(0);
done(err);
});
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('sends errors properly', function (done) {
it('sends errors properly', function () {
var config = {
context: './test/fixtures/test3',
entry: './index',
plugins: [
new StyleLintPlugin({
quiet: true,
configFile: configFilePath
})
]
entry: './index'
};

pack(assign({}, baseConfig, config), function (err, stats) {
expect(err).to.not.exist;
expect(stats.compilation.errors).to.have.length(1);
done(err);
});
return pack(assign({}, baseConfig, config))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(1);
});
});

it('fails on errors', function () {
it('fails on errors when asked to', function () {
var config = {
context: './test/fixtures/test3',
entry: './index',
Expand All @@ -77,62 +58,19 @@ describe('stylelint-webpack-plugin', function () {
]
};

return expect(new Promise(function (resolve, reject) {
var compiler = webpack(assign({}, baseConfig, config));
compiler.outputFileSystem = outputFileSystem;
compiler.run(function (err) {
reject(err);
});
})).to.eventually.be.rejectedWith('Error: Failed because of a stylelint error.\n');
});

it('can specify a JSON config file via config', function (done) {
var config = {
context: './test/fixtures/test5',
entry: './index',
plugins: [
new StyleLintPlugin({
configFile: configFilePath,
quiet: true
})
]
};

pack(assign({}, baseConfig, config), function (err, stats) {
expect(err).to.not.exist;
expect(stats.compilation.errors).to.have.length(0);
done(err);
});
expect(pack(assign({}, baseConfig, config)))
.to.eventually.be.rejectedWith('Error: Failed because of a stylelint error.\n');
});

it('works with multiple source files', function (done) {
it('works with multiple source files', function () {
var config = {
context: './test/fixtures/test7',
entry: './index'
};

pack(assign({}, baseConfig, config), function (err, stats) {
expect(err).to.not.exist;
expect(stats.compilation.errors).to.have.length(2);
done(err);
});
return pack(assign({}, baseConfig, config))
.then(function (stats) {
expect(stats.compilation.errors).to.have.length(2);
});
});

// it('should work with multiple context', function(done) {
// var config = {
// context: './test/fixtures/test5',
// entry: './index',
// plugins: [ new StyleLintPlugin({
// configFile: configFilePath,
// context: ['./test/testFiles/test5', './test/testFiles/test7']
// })]
// };

// pack(assign({}, baseConfig, config), function (err, stats) {
// expect(err).to.not.exist;
// expect(stats.compilation.errors.length).to.equal(0);
// expect(stats.compilation.warnings.length).not.to.equal(0);
// done(err);
// });
// });
});