diff --git a/.travis.yml b/.travis.yml index ecef40d..6cd8692 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,17 @@ language: node_js node_js: + - 7 - 6 - 5 - 4 +env: + - WEBPACK_VERSION=2 + - WEBPACK_VERSION=1 + matrix: fast_finish: true + +before_script: + - npm install webpack@$WEBPACK_VERSION diff --git a/package.json b/package.json index bc2f33e..2ce0195 100644 --- a/package.json +++ b/package.json @@ -25,16 +25,22 @@ "object-hash": "^1.1.4" }, "devDependencies": { + "ava": "^0.17.0", "eslint": "^3.0.0", "eslint-friendly-formatter": "^2.0.4", "npmpub": "^3.0.1", - "tape": "^4.0.0", - "webpack": "^1.8.4" + "webpack": "^2.2.0" }, "scripts": { "lint": "eslint .", - "tape": "tape test/*.js", - "test": "npm run lint && npm run tape", + "ava": "ava", + "test": "npm run lint && npm run ava", "release": "npmpub" + }, + "ava": { + "files": [ + "test/*.js" + ], + "verbose": true } } diff --git a/test/autofix.js b/test/autofix.js index 1c0c0b6..a056bd6 100644 --- a/test/autofix.js +++ b/test/autofix.js @@ -1,22 +1,20 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") var fs = require("fs") // clone the "fixable" file, so that we do not lose the original contents // when the fixes are applied to disk -test("setup", function(t) { +test.before(function() { fs .createReadStream("./test/fixtures/fixable.js") .pipe(fs.createWriteStream("./test/fixtures/fixable-clone.js")) - - t.end() }) -test("loader doesn't throw error if file ok after auto-fixing", function(t) { - webpack(assign({}, - conf, +test.cb("loader doesn't throw error if file ok after auto-fixing", +function(t) { + t.plan(2) + webpack(conf( { entry: "./test/fixtures/fixable-clone.js", module: { @@ -35,15 +33,14 @@ test("loader doesn't throw error if file ok after auto-fixing", function(t) { throw err } // console.log(stats.compilation.errors) - t.notOk(stats.hasErrors(), "a good file doesn't give any error") + t.false(stats.hasErrors(), "a good file doesn't give any error") // console.log(stats.compilation.warnings) - t.notOk(stats.hasWarnings(), "a good file doesn't give any warning") + t.false(stats.hasWarnings(), "a good file doesn't give any warning") t.end() }) }) // remove the clone -test("teardown", function(t) { +test.after.always(function() { fs.unlinkSync("./test/fixtures/fixable-clone.js") - t.end() }) diff --git a/test/error.js b/test/error.js index 3025b31..c1a3be7 100644 --- a/test/error.js +++ b/test/error.js @@ -1,11 +1,10 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") -test("eslint-loader can return error if file is bad", function(t) { - webpack(assign({}, - conf, +test.cb("eslint-loader can return error if file is bad", function(t) { + t.plan(1) + webpack(conf( { entry: "./test/fixtures/error.js", } @@ -16,7 +15,7 @@ test("eslint-loader can return error if file is bad", function(t) { } // console.log(stats.compilation.errors) - t.ok( + t.true( stats.hasErrors(), "a file that contains eslint errors should return error" ) diff --git a/test/eslintignore.js b/test/eslintignore.js index 73bfd66..9168304 100644 --- a/test/eslintignore.js +++ b/test/eslintignore.js @@ -1,18 +1,17 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") -test("eslint-loader ignores files present in .eslintignore", function(t) { - webpack(assign({}, - conf, +test.cb("eslint-loader ignores files present in .eslintignore", function(t) { + t.plan(1) + webpack(conf( { entry: "./test/fixtures/ignore.js", - eslint: assign({}, conf.eslint, { - // we want to enable ignore, so eslint will parse .eslintignore and - // should skip the file specified above - ignore: true, - }), + }, + { + // we want to enable ignore, so eslint will parse .eslintignore and + // should skip the file specified above + ignore: true, } ), function(err, stats) { @@ -21,7 +20,7 @@ test("eslint-loader ignores files present in .eslintignore", function(t) { } // console.log(stats.compilation.warnings) - t.notOk(stats.hasWarnings(), "an ignored doesn't give a warning") + t.false(stats.hasWarnings(), "an ignored doesn't give a warning") t.end() }) }) diff --git a/test/force-emit-error.js b/test/force-emit-error.js new file mode 100644 index 0000000..aa83fc3 --- /dev/null +++ b/test/force-emit-error.js @@ -0,0 +1,29 @@ +var test = require("ava") +var webpack = require("webpack") +var conf = require("./utils/conf") + +test.cb("eslint-loader can force to emit error", function(t) { + t.plan(2) + webpack(conf( + { + entry: "./test/fixtures/warn.js", + }, + { + emitError: true, + } + ), + function(err, stats) { + if (err) { + throw err + } + + // console.log(stats.compilation.errors) + t.true(stats.hasErrors(), "a file should return error if asked") + // console.log(stats.compilation.warnings) + t.false( + stats.hasWarnings(), + "a file should return no warning if error asked" + ) + t.end() + }) +}) diff --git a/test/force-emit-warning.js b/test/force-emit-warning.js new file mode 100644 index 0000000..172d3a4 --- /dev/null +++ b/test/force-emit-warning.js @@ -0,0 +1,26 @@ +var test = require("ava") +var webpack = require("webpack") +var conf = require("./utils/conf") + +test.cb("eslint-loader can force to emit warning", function(t) { + t.plan(2) + webpack(conf( + { + entry: "./test/fixtures/error.js", + }, + { + emitWarning: true, + } + ), + function(err, stats) { + if (err) { + throw err + } + + // console.log(stats.compilation.warnings) + t.true(stats.hasWarnings(), "a file should return warning if asked") + // console.log(stats.compilation.errors) + t.false(stats.hasErrors(), "a file should return no error if error asked") + t.end() + }) +}) diff --git a/test/force-emit.js b/test/force-emit.js deleted file mode 100644 index 7a85a48..0000000 --- a/test/force-emit.js +++ /dev/null @@ -1,53 +0,0 @@ -var test = require("tape") -var webpack = require("webpack") -var assign = require("object-assign") -var conf = require("./utils/conf") - -test("eslint-loader can force to emit error", function(t) { - webpack(assign({}, - conf, - { - entry: "./test/fixtures/warn.js", - eslint: assign({}, conf.eslint, { - emitError: true, - }), - } - ), - function(err, stats) { - if (err) { - throw err - } - - // console.log(stats.compilation.errors) - t.ok(stats.hasErrors(), "a file should return error if asked") - // console.log(stats.compilation.warnings) - t.notOk( - stats.hasWarnings(), - "a file should return no warning if error asked" - ) - t.end() - }) -}) - -test("eslint-loader can force to emit warning", function(t) { - webpack(assign({}, - conf, - { - entry: "./test/fixtures/error.js", - eslint: assign({}, conf.eslint, { - emitWarning: true, - }), - } - ), - function(err, stats) { - if (err) { - throw err - } - - // console.log(stats.compilation.warnings) - t.ok(stats.hasWarnings(), "a file should return warning if asked") - // console.log(stats.compilation.errors) - t.notOk(stats.hasErrors(), "a file should return no error if error asked") - t.end() - }) -}) diff --git a/test/formatter-custom.js b/test/formatter-custom.js new file mode 100644 index 0000000..731f8d9 --- /dev/null +++ b/test/formatter-custom.js @@ -0,0 +1,34 @@ +/* eslint-disable no-console */ +var test = require("ava") +var webpack = require("webpack") +var conf = require("./utils/conf") + +test.cb("eslint-loader can use custom formatter", function(t) { + t.plan(1) + webpack(conf( + { + entry: "./test/fixtures/error.js", + }, + { + formatter: require("eslint-friendly-formatter"), + } + ), + function(err, stats) { + if (err) { + throw err + } + + console.log("### Here is a example of another formatter") + console.log( + "# " + + stats.compilation.errors[0].message + .split("\n") + .join("\n# ") + ) + t.truthy( + stats.compilation.errors[0].message, + "webpack have some output with custom formatters" + ) + t.end() + }) +}) diff --git a/test/formatter-eslint.js b/test/formatter-eslint.js new file mode 100644 index 0000000..47a2ce3 --- /dev/null +++ b/test/formatter-eslint.js @@ -0,0 +1,28 @@ +/* eslint-disable no-console */ +var test = require("ava") +var webpack = require("webpack") +var conf = require("./utils/conf") + +test.cb("eslint-loader can use eslint formatter", function(t) { + t.plan(1) + webpack(conf( + { + entry: "./test/fixtures/error.js", + } + ), + function(err, stats) { + if (err) { + throw err + } + + console.log("### Here is a example of the default formatter") + console.log( + "# " + + stats.compilation.errors[0].message + .split("\n") + .join("\n# ") + ) + t.truthy(stats.compilation.errors[0].message, "webpack have some output") + t.end() + }) +}) diff --git a/test/formatter-write.js b/test/formatter-write.js new file mode 100644 index 0000000..bd3a151 --- /dev/null +++ b/test/formatter-write.js @@ -0,0 +1,54 @@ +/* eslint-disable no-console */ +var test = require("ava") +var webpack = require("webpack") +var conf = require("./utils/conf") +var fs = require("fs") + +test.cb("eslint-loader can be configured to write eslint results to a file", +function(t) { + t.plan(2) + + var outputFilename = "outputReport.txt" + var config = conf( + { + entry: "./test/fixtures/error.js", + }, + { + formatter: require("eslint/lib/formatters/checkstyle"), + outputReport: { + filePath: outputFilename, + }, + } + ) + + webpack(config, + function(err, stats) { + if (err) { + throw err + } + + console.log("### Here is a the output of the formatter") + console.log( + "# " + + stats.compilation.errors[0].message + .split("\n") + .join("\n# ") + ) + + fs.readFile(config.output.path + outputFilename, + "utf8", function(err, contents) { + if (err) { + t.fail("Expected file to have been created") + } + else { + t.pass("File has been created") + + t.is(stats.compilation.errors[0].message, contents, + "File Contents should equal output") + } + + t.end() + + }) + }) +}) diff --git a/test/formatter.js b/test/formatter.js deleted file mode 100644 index 6b7710b..0000000 --- a/test/formatter.js +++ /dev/null @@ -1,108 +0,0 @@ -var test = require("tape") -var webpack = require("webpack") -var assign = require("object-assign") -var conf = require("./utils/conf") -var fs = require("fs") - -/* eslint-disable no-console */ -test("eslint-loader can use eslint formatter", function(t) { - webpack(assign({}, - conf, - { - entry: "./test/fixtures/error.js", - } - ), - function(err, stats) { - if (err) { - throw err - } - - console.log("### Here is a example of the default formatter") - console.log( - "# " + - stats.compilation.errors[0].message - .split("\n") - .join("\n# ") - ) - t.ok(stats.compilation.errors[0].message, "webpack have some output") - t.end() - }) -}) - -test("eslint-loader can use custom formatter", function(t) { - webpack(assign({}, - conf, - { - entry: "./test/fixtures/error.js", - eslint: assign({}, conf.eslint, { - formatter: require("eslint-friendly-formatter"), - }), - } - ), - function(err, stats) { - if (err) { - throw err - } - - console.log("### Here is a example of another formatter") - console.log( - "# " + - stats.compilation.errors[0].message - .split("\n") - .join("\n# ") - ) - t.ok( - stats.compilation.errors[0].message, - "webpack have some output with custom formatters" - ) - t.end() - }) -}) - -test("eslint-loader cant be configured to write eslint results to a file", -function(t) { - - var outputFilename = "outputReport.txt" - - webpack(assign({}, - conf, - { - entry: "./test/fixtures/error.js", - eslint: assign({}, conf.eslint, { - formatter: require("eslint/lib/formatters/checkstyle"), - outputReport: { - filePath: outputFilename, - }, - }), - } - ), - function(err, stats) { - if (err) { - throw err - } - - console.log("### Here is a the outpur of the formatter") - console.log( - "# " + - stats.compilation.errors[0].message - .split("\n") - .join("\n# ") - ) - - fs.readFile(conf.output.path + outputFilename, - "utf8", function(err, contents) { - if (err) { - t.fail("Expected file to have been created") - } - else { - t.pass("File has been created") - - t.equal(stats.compilation.errors[0].message, contents, - "File Contents should equal output") - } - - }) - - t.end() - }) -}) diff --git a/test/multiple-engines.js b/test/multiple-engines.js index cf8185a..c524637 100644 --- a/test/multiple-engines.js +++ b/test/multiple-engines.js @@ -1,11 +1,10 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") -test("eslint-loader will create an engine for each unique config", function(t) { // eslint-disable-line max-len - webpack(assign({}, - conf, +test.cb("eslint-loader will create an engine for each unique config", function(t) { // eslint-disable-line max-len + t.plan(3) + webpack(conf( { entry: "./test/fixtures/good.js", module: { @@ -39,17 +38,17 @@ test("eslint-loader will create an engine for each unique config", function(t) { throw err } - t.ok( + t.true( stats.compilation.warnings.length === 2, "should report an error for each config" ) - t.ok( + t.truthy( stats.compilation.warnings.find(warning => /quotes/.test(warning)), "should have a warning about quotes" ) - t.ok( + t.truthy( stats.compilation.warnings.find(warning => /semi/.test(warning)), "should have a warning about semi" ) diff --git a/test/ok.js b/test/ok.js index ca1b228..04f9cbc 100644 --- a/test/ok.js +++ b/test/ok.js @@ -1,11 +1,10 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") -test("eslint-loader don't throw error if file is ok", function(t) { - webpack(assign({}, - conf, +test.cb("eslint-loader don't throw error if file is ok", function(t) { + t.plan(2) + webpack(conf( { entry: "./test/fixtures/good.js", } @@ -16,9 +15,9 @@ test("eslint-loader don't throw error if file is ok", function(t) { } // console.log(stats.compilation.errors) - t.notOk(stats.hasErrors(), "a good file doesn't give any error") + t.false(stats.hasErrors(), "a good file doesn't give any error") // console.log(stats.compilation.warnings) - t.notOk(stats.hasWarnings(), "a good file doesn't give any warning") + t.false(stats.hasWarnings(), "a good file doesn't give any warning") t.end() }) }) diff --git a/test/parameters.js b/test/parameters.js index 72ad014..a5136f7 100644 --- a/test/parameters.js +++ b/test/parameters.js @@ -1,11 +1,10 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") -test("eslint-loader supports query strings parameters", function(t) { - webpack(assign({}, - conf, +test.cb("eslint-loader supports query strings parameters", function(t) { + t.plan(2) + webpack(conf( { entry: "./test/fixtures/good-semi.js", module: { @@ -25,9 +24,9 @@ test("eslint-loader supports query strings parameters", function(t) { } // console.log(stats.compilation.errors) - t.notOk(stats.hasErrors(), "a good file doesn't give any error") + t.false(stats.hasErrors(), "a good file doesn't give any error") // console.log(stats.compilation.warnings) - t.notOk(stats.hasWarnings(), "a good file doesn't give any warning") + t.false(stats.hasWarnings(), "a good file doesn't give any warning") t.end() }) }) diff --git a/test/quiet.js b/test/quiet.js index d8bdc8f..b4faf4c 100644 --- a/test/quiet.js +++ b/test/quiet.js @@ -1,18 +1,17 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") -test( +test.cb( "eslint-loader only returns errors and not warnings if quiet is set", function(t) { - webpack(assign({}, - conf, + t.plan(2) + webpack(conf( { entry: "./test/fixtures/warn.js", - eslint: assign({}, conf.eslint, { - quiet: true, - }), + }, + { + quiet: true, } ), function(err, stats) { @@ -21,13 +20,13 @@ test( } // console.log(stats.compilation.warnings) - t.notOk( + t.false( stats.hasWarnings(), "a file that contains eslint warning should return nothing if quiet " + "option is true" ) // console.log(stats.compilation.errors) - t.notOk( + t.false( stats.hasErrors(), "a file that contains eslint warning should return no error if it " + "contains only warning in quiet mode" diff --git a/test/utils/conf.js b/test/utils/conf.js index d1b05e9..1faabd4 100644 --- a/test/utils/conf.js +++ b/test/utils/conf.js @@ -1,4 +1,7 @@ -module.exports = { +var webpack = require("webpack") +var assign = require("object-assign") + +var DEFAULT_CONFIG = { output: { path: "./test/output/", filename: "bundle.js", @@ -12,10 +15,38 @@ module.exports = { }, ], }, - // this disables the use of .eslintignore, since it contains the fixtures - // folder to skip it on the global linting, but here we want the opposite - // (we only use .eslintignore on the test that checks this) - eslint: { - ignore: false, - }, +} + +/** + * Returns a valid config for both webpack versions 1 and 2. + * @param webpackConf Additional webpack config to apply/override to the default + * @param loaderConf Additional eslint config to apply/override to the default + * @returns {Object} + */ +module.exports = function conf(webpackConf, loaderConf) { + + loaderConf = { + eslint: assign({ + // this disables the use of .eslintignore, since it contains the fixtures + // folder to skip it on the global linting, but here we want the opposite + // (we only use .eslintignore on the test that checks this) + ignore: false, + }, loaderConf), + } + + // webpack v1 allows loader option to be added directly to the root webpack + // config object + // webpack v2 requires them to be added via the LoaderOptionsPlugin + return assign(DEFAULT_CONFIG, webpackConf, + process.env.WEBPACK_VERSION === "1" ? + loaderConf : { + plugins: [ + new webpack.LoaderOptionsPlugin({ + exclude: /node_modules/, + options: loaderConf, + }), + ], + } + ) + } diff --git a/test/warning.js b/test/warning.js index 35f4759..5dc9f6d 100644 --- a/test/warning.js +++ b/test/warning.js @@ -1,11 +1,10 @@ -var test = require("tape") +var test = require("ava") var webpack = require("webpack") -var assign = require("object-assign") var conf = require("./utils/conf") -test("eslint-loader can return warning", function(t) { - webpack(assign({}, - conf, +test.cb("eslint-loader can return warning", function(t) { + t.plan(2) + webpack(conf( { entry: "./test/fixtures/warn.js", } @@ -16,13 +15,13 @@ test("eslint-loader can return warning", function(t) { } // console.log(stats.compilation.warnings) - t.ok( + t.true( stats.hasWarnings(), "a file that contains eslint warning should return warning" ) // console.log(stats.compilation.errors) - t.notOk( + t.false( stats.hasErrors(), "a bad file should return no error if it contains only warning by default" )