Skip to content

Commit

Permalink
Rework tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Iaconis committed Mar 10, 2016
1 parent 55d9773 commit 1b21462
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 36 deletions.
23 changes: 0 additions & 23 deletions Brocfile.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"babelify": "babel lib --out-dir build",
"prepublish": "npm run babelify",
"lint": "eslint .",
"test": "npm run babelify && rm -rf lintbuild/ && ./node_modules/broccoli-cli/bin/broccoli build lintbuild > broccoli-build.out && node node_modules/mocha/bin/mocha lintbuild/test.js"
"babelify-tests": "babel test --ignore fixture --out-dir build/test",
"test": "npm run babelify && npm run babelify-tests && npm run lint && node_modules/mocha/bin/mocha build/test/test.js"
},
"repository": {
"type": "git",
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function noNewLineBeforeReturn() {
const foo = 'foo';

console.log(foo);
}

noNewLineBeforeReturn();
35 changes: 35 additions & 0 deletions test/helpers/run-eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const broccoli = require('broccoli');
const eslint = require('../../index');

module.exports = function runEslint(path, _options) {
const options = _options || {};
const buildLog = [];
const consoleLog = console.log;

// stub console.log so we can get the formatter's output
console.log = function appendToBuildLog(...args) {
const text = args.join(' ');

buildLog.push(text);
};

// default options
options.format = options.format || 'eslint/lib/formatters/compact';
options.options = options.options || {};
options.options.ignore = options.options.ignore || false;

const tree = eslint(path, options);
const builder = new broccoli.Builder(tree);
const promise = builder.build().then(function builderThen() {
return buildLog.join('\n');
});

promise.finally(function builderCleanup() {
builder.cleanup();

// restore the original console.log
console.log = consoleLog;
});

return promise;
};
49 changes: 37 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
const assert = require('assert');
const fs = require('fs');
const rimraf = require('rimraf');
const runEslint = require('./helpers/run-eslint');
const NOT_FOUND = -1;
const FIXTURES = 'test/fixture';

afterEach(function afterEach() {
rimraf.sync('temp');
rimraf.sync('broccoli-build.out');

it('should report errors', function shouldReportErrors() {

// lint test fixtures
const promise = runEslint(FIXTURES);

return promise.then(function assertLinting(buildLog) {
assert(buildLog.indexOf('(camelcase)') !== NOT_FOUND, 'Used eslint validation - camel case');
assert(buildLog.indexOf('fixture/1.js') !== NOT_FOUND, 'Shows filepath');
assert(buildLog.indexOf('(no-console)') !== NOT_FOUND, 'Used relative eslint config file');
});
});

it('should accept rule paths', function shouldAcceptRulePaths() {
// lint test fixtures using a custom rule
const promise = runEslint(FIXTURES, {
options: {
rulePaths: ['conf/rules']
}
});

return promise.then(function assertLinting(buildLog) {
assert(buildLog.indexOf('testing custom rules') !== NOT_FOUND, 'Used custom rule');
});
});

it('should reported errors', function shouldReportErrors() {
const buildLog = fs.readFileSync('broccoli-build.out').toString();
const NOT_FOUND = -1;
it('should accept config file path', function shouldAcceptConfigFile() {
// lint test fixtures using a config file at a non-default path
const promise = runEslint(FIXTURES, {
options: {
configFile: 'conf/eslint.json'
}
});

assert(buildLog.indexOf('Strings must use doublequote.') !== NOT_FOUND, 'Used eslint validation - strings');
assert(buildLog.indexOf('is not in camel case') !== NOT_FOUND, 'Used eslint validation - camel case');
assert(buildLog.indexOf('testing custom rules') !== NOT_FOUND, 'Used custom rulesdir rules');
assert(buildLog.indexOf('fixture/1.js') !== NOT_FOUND, 'Shows filepath');
return promise.then(function assertLinting(buildLog) {
assert(buildLog.indexOf('Strings must use doublequote.') !== NOT_FOUND, 'Used alternate config');
});
});

0 comments on commit 1b21462

Please sign in to comment.