From 54be1e882568bf27a3283f26f547a02610e56ec9 Mon Sep 17 00:00:00 2001 From: Jason Kurian Date: Fri, 12 Aug 2016 22:13:56 -0400 Subject: [PATCH] tests: promisify the in-memory compiler and remove redundant tests --- lib/run-compilation.js | 2 +- test/helpers/pack.js | 24 ++++++++++ test/index.js | 106 +++++++++-------------------------------- 3 files changed, 47 insertions(+), 85 deletions(-) create mode 100644 test/helpers/pack.js diff --git a/lib/run-compilation.js b/lib/run-compilation.js index 908f9be..2dbf202 100644 --- a/lib/run-compilation.js +++ b/lib/run-compilation.js @@ -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 = []; diff --git a/test/helpers/pack.js b/test/helpers/pack.js new file mode 100644 index 0000000..202ead6 --- /dev/null +++ b/test/helpers/pack.js @@ -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); + }); + }); +}; diff --git a/test/index.js b/test/index.js index 18fa8c8..0885204 100644 --- a/test/index.js +++ b/test/index.js @@ -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 = { @@ -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', @@ -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); - // }); - // }); });