From 1d11fa7883609355736d74871b45dc7f09982f1c Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 30 Mar 2017 18:01:25 -0700 Subject: [PATCH 1/4] Add integration tests using "broccoli-test-helper" --- package.json | 2 ++ test/eslint-test.js | 68 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 test/eslint-test.js diff --git a/package.json b/package.json index 3f0da75..36b8789 100644 --- a/package.json +++ b/package.json @@ -46,8 +46,10 @@ "broccoli-merge-trees": "^2.0.0", "broccoli-source": "^1.1.0", "broccoli-stew": "^1.2.0", + "broccoli-test-helper": "^1.1.0", "chai": "^3.5.0", "chai-as-promised": "^6.0.0", + "co": "^4.6.0", "mocha": "^3.0.1", "sinon": "^2.0.0", "sinon-chai": "^2.8.0" diff --git a/test/eslint-test.js b/test/eslint-test.js new file mode 100644 index 0000000..bf1a150 --- /dev/null +++ b/test/eslint-test.js @@ -0,0 +1,68 @@ +'use strict'; + +const path = require('path'); +const expect = require('./chai').expect; +const co = require('co'); +const testHelpers = require('broccoli-test-helper'); +const eslint = require('..'); + +const createBuilder = testHelpers.createBuilder; +const createTempDir = testHelpers.createTempDir; + +describe('broccoli-lint-eslint', function() { + let input, output, console; + + beforeEach(co.wrap(function *() { + input = yield createTempDir(); + console = { + log(line) {}, + }; + })); + + afterEach(co.wrap(function *() { + yield input.dispose(); + if (output) { + yield output.dispose(); + } + })); + + it('logs errors to the console', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'error', 'no-unused-vars': 'warn' } };\n`, + 'a.js': `console.log('foo');\n`, + 'b.js': `var foo = 5;\n`, + }); + + let format = 'eslint/lib/formatters/compact'; + + let messages = []; + let console = { + log(message) { + messages.push(message); + } + }; + + output = createBuilder(eslint(input.path(), { format, console })); + + yield output.build(); + + expect(messages.join('')) + .to.contain(`a.js: line 1, col 1, Error - Unexpected console statement. (no-console)\n`) + .to.contain(`b.js: line 1, col 5, Warning - 'foo' is assigned a value but never used. (no-unused-vars)\n`); + })); + + + it('does not generate test files by default', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'error', 'no-unused-vars': 'warn' } };\n`, + 'a.js': `console.log('foo');\n`, + 'b.js': `var foo = 5;\n`, + }); + + output = createBuilder(eslint(input.path(), { console })); + + yield output.build(); + + expect(Object.keys(output.read())).to.deep.equal(['.eslintrc.js', 'a.js', 'b.js']); + })); +}); From bb3377973f35d5ccf6655728583106c540f15796 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 30 Mar 2017 18:01:50 -0700 Subject: [PATCH 2/4] Add "throwOnError" and "throwOnWarn" tests --- test/eslint-test.js | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/eslint-test.js b/test/eslint-test.js index bf1a150..d63b7ce 100644 --- a/test/eslint-test.js +++ b/test/eslint-test.js @@ -65,4 +65,74 @@ describe('broccoli-lint-eslint', function() { expect(Object.keys(output.read())).to.deep.equal(['.eslintrc.js', 'a.js', 'b.js']); })); + + describe('throwOnError', function() { + it('throw an error for the first encountered error', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'error' } };\n`, + 'a.js': `console.log('foo');\n`, + }); + + output = createBuilder(eslint(input.path(), { console, throwOnError: true })); + + yield expect(output.build()).to.be.rejectedWith('rules violation with `error` severity level'); + })); + + it('does not throw errors for warning', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'warn' } };\n`, + 'a.js': `console.log('foo');\n`, + }); + + output = createBuilder(eslint(input.path(), { console, throwOnError: true })); + + yield expect(output.build()).to.be.fulfilled; + })); + + it('does not throw errors for disabled rules', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'off' } };\n`, + 'a.js': `console.log('foo');\n`, + }); + + output = createBuilder(eslint(input.path(), { console, throwOnError: true })); + + yield expect(output.build()).to.be.fulfilled; + })); + }); + + describe('throwOnWarn', function() { + it('throw an error for the first encountered error', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'error' } };\n`, + 'a.js': `console.log('foo');\n`, + }); + + output = createBuilder(eslint(input.path(), { console, throwOnWarn: true })); + + yield expect(output.build()).to.be.rejectedWith('rules violation with `error` severity level'); + })); + + it('throw an error for the first encountered warning', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'warn' } };\n`, + 'a.js': `console.log('foo');\n`, + }); + + output = createBuilder(eslint(input.path(), { console, throwOnWarn: true })); + + yield expect(output.build()).to.be.rejectedWith('rules violation with `warn` severity level'); + })); + + it('does not throw errors for disabled rules', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'off' } };\n`, + 'a.js': `console.log('foo');\n`, + }); + + output = createBuilder(eslint(input.path(), { console, throwOnWarn: true })); + + yield expect(output.build()).to.be.fulfilled; + })); + }); }); From 05de07ae95fab24f6c6df2db39ca8527aee72f58 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 30 Mar 2017 18:12:39 -0700 Subject: [PATCH 3/4] Add "testGenerator" tests --- test/eslint-test.js | 92 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/test/eslint-test.js b/test/eslint-test.js index d63b7ce..7ca42aa 100644 --- a/test/eslint-test.js +++ b/test/eslint-test.js @@ -66,6 +66,98 @@ describe('broccoli-lint-eslint', function() { expect(Object.keys(output.read())).to.deep.equal(['.eslintrc.js', 'a.js', 'b.js']); })); + describe('testGenerator', function() { + it('qunit: generates QUnit tests', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'error', 'no-unused-vars': 'warn' } };\n`, + 'a.js': `console.log('foo');\n`, + 'b.js': `var foo = 5;\n`, + }); + + output = createBuilder(eslint(input.path(), { console, testGenerator: 'qunit' })); + + yield output.build(); + + let result = output.read(); + expect(Object.keys(result)).to.deep.equal(['.eslintrc.lint-test.js', 'a.lint-test.js', 'b.lint-test.js']); + expect(result['a.lint-test.js'].trim()).to.equal([ + `QUnit.module('ESLint | a.js');`, + `QUnit.test('should pass ESLint', function(assert) {`, + ` assert.expect(1);`, + ` assert.ok(false, 'a.js should pass ESLint\\n\\n1:1 - Unexpected console statement. (no-console)');`, + `});`, + ].join('\n')); + })); + + it('mocha: generates Mocha tests', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'error', 'no-unused-vars': 'warn' } };\n`, + 'a.js': `console.log('foo');\n`, + 'b.js': `var foo = 5;\n`, + }); + + output = createBuilder(eslint(input.path(), { console, testGenerator: 'mocha' })); + + yield output.build(); + + let result = output.read(); + expect(Object.keys(result)).to.deep.equal(['.eslintrc.lint-test.js', 'a.lint-test.js', 'b.lint-test.js']); + expect(result['a.lint-test.js'].trim()).to.equal([ + `describe('ESLint | a.js', function() {`, + ` it('should pass ESLint', function() {`, + ` // ESLint failed`, + ` var error = new chai.AssertionError('a.js should pass ESLint\\n\\n1:1 - Unexpected console statement. (no-console)');`, + ` error.stack = undefined;`, + ` throw error;`, + ` });`, + `});`, + ].join('\n')); + })); + + it('custom: generates tests via custom test generator function', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'error', 'no-unused-vars': 'warn' } };\n`, + 'a.js': `console.log('foo');\n`, + 'b.js': `var foo = 5;\n`, + }); + + let args = []; + function testGenerator() { + args.push(arguments); + } + + output = createBuilder(eslint(input.path(), { console, testGenerator })); + + yield output.build(); + + expect(args).to.have.lengthOf(3); + expect(args[0][0]).to.equal('.eslintrc.js'); + expect(args[1][0]).to.equal('a.js'); + expect(args[2][0]).to.equal('b.js'); + + let results = args[1][2]; + expect(results.filePath).to.match(/a\.js$/); + delete results.filePath; + + expect(results).to.deep.equal({ + 'errorCount': 1, + 'messages': [{ + 'column': 1, + 'endColumn': 12, + 'endLine': 1, + 'line': 1, + 'message': 'Unexpected console statement.', + 'nodeType': 'MemberExpression', + 'ruleId': 'no-console', + 'severity': 2, + 'source': 'console.log(\'foo\');', + }], + 'source': 'console.log(\'foo\');\n', + 'warningCount': 0, + }); + })); + }); + describe('throwOnError', function() { it('throw an error for the first encountered error', co.wrap(function *() { input.write({ From b69e3af1f4d61e09d7e2b7651cff147c709e58d5 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Thu, 30 Mar 2017 18:30:49 -0700 Subject: [PATCH 4/4] Add ".eslintignore" test --- test/eslint-test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/eslint-test.js b/test/eslint-test.js index 7ca42aa..e4a0dde 100644 --- a/test/eslint-test.js +++ b/test/eslint-test.js @@ -227,4 +227,27 @@ describe('broccoli-lint-eslint', function() { yield expect(output.build()).to.be.fulfilled; })); }); + + describe('.eslintignore', function() { + // this doesn't seem to work... :( + it.skip('excludes files from being linted', co.wrap(function *() { + input.write({ + '.eslintrc.js': `module.exports = { rules: { 'no-console': 'error', 'no-unused-vars': 'warn' } };\n`, + '.eslintignore': `a.js\n`, + 'a.js': `console.log('foo');\n`, + 'b.js': `var foo = 5;\n`, + }); + + output = createBuilder(eslint(input.path(), { console, testGenerator: 'qunit' })); + + yield output.build(); + + let result = output.read(); + expect(Object.keys(result)).to.deep.equal([ + '.eslintignore', + '.eslintrc.lint-test.js', + 'b.lint-test.js', + ]); + })); + }); });