From 7ecec182ba4f862b68ed291c6b3ad14cb7c334d6 Mon Sep 17 00:00:00 2001 From: Ari Porad Date: Fri, 25 Dec 2015 20:38:50 -0800 Subject: [PATCH 1/3] Find relative root of all test files for displaing directories Fixes #249. --- api.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/api.js b/api.js index 0101e9366..4705b59a5 100644 --- a/api.js +++ b/api.js @@ -31,6 +31,7 @@ function Api(files, options) { this.stats = []; this.tests = []; this.files = files || []; + this.base = ''; Object.keys(Api.prototype).forEach(function (key) { this[key] = this[key].bind(this); @@ -99,6 +100,23 @@ Api.prototype._handleTest = function (test) { this.emit('test', test); }; +Api.prototype._findBase = function (files) { + this.base = files.reduce(function (base, file) { + file = path.relative('.', file); + file = file.split(path.sep); + if (base === false) return file; + return base.filter(function (part, i) { + return file[i].toLowerCase() === part.toLowerCase(); + }); + }, false).join(path.sep); + + if (this.base === '' || this.base === '.') { + this.base = this.files[0] || 'test'; + } + + this.base += path.sep; +}; + Api.prototype._prefixTitle = function (file) { if (this.fileCount === 1) { return ''; @@ -106,17 +124,10 @@ Api.prototype._prefixTitle = function (file) { var separator = ' ' + chalk.gray.dim(figures.pointerSmall) + ' '; - var base = path.dirname(this.files[0]); - - if (base === '.') { - base = this.files[0] || 'test'; - } - - base += path.sep; - var prefix = path.relative('.', file) - .replace(base, '') + .replace(this.base, '') .replace(/\.spec/, '') + .replace(/\.test/, '') .replace(/test\-/g, '') .replace(/\.js$/, '') .split(path.sep) @@ -143,6 +154,8 @@ Api.prototype.run = function () { self.fileCount = files.length; + self._findBase(files); + var tests = files.map(self._runFile); // receive test count from all files and then run the tests From 8401f8afd6877626e1db4f8710bd990fa6aa0d8f Mon Sep 17 00:00:00 2001 From: Ari Porad Date: Sat, 26 Dec 2015 08:49:11 -0800 Subject: [PATCH 2/3] Add tests for output for nested dirs and recursivity Refs #249. --- api.js | 4 +++- test/api.js | 33 ++++++++++++++++++++++----- test/fixture/subdir/failing-subdir.js | 8 +++++++ test/fixture/subdir/in-a-subdir.js | 5 ++++ 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 test/fixture/subdir/failing-subdir.js create mode 100644 test/fixture/subdir/in-a-subdir.js diff --git a/api.js b/api.js index 4705b59a5..f0e93d921 100644 --- a/api.js +++ b/api.js @@ -104,7 +104,9 @@ Api.prototype._findBase = function (files) { this.base = files.reduce(function (base, file) { file = path.relative('.', file); file = file.split(path.sep); - if (base === false) return file; + if (base === false) { + return file; + } return base.filter(function (part, i) { return file[i].toLowerCase() === part.toLowerCase(); }); diff --git a/test/api.js b/test/api.js index 53c3f7b94..b688fe9b0 100644 --- a/test/api.js +++ b/test/api.js @@ -38,19 +38,21 @@ test('async/await support', function (t) { }); test('test title prefixes', function (t) { - t.plan(5); + t.plan(6); var separator = ' ' + figures.pointerSmall + ' '; var files = [ path.join(__dirname, 'fixture/async-await.js'), path.join(__dirname, 'fixture/es2015.js'), - path.join(__dirname, 'fixture/generators.js') + path.join(__dirname, 'fixture/generators.js'), + path.join(__dirname, 'fixture/subdir/in-a-subdir.js') ]; var expected = [ ['async-await', 'async function'].join(separator), ['async-await', 'arrow async function'].join(separator), ['es2015', '[anonymous]'].join(separator), - ['generators', 'generator function'].join(separator) + ['generators', 'generator function'].join(separator), + ['subdir', 'in-a-subdir', 'subdir'].join(separator) ]; var index; @@ -64,8 +66,7 @@ test('test title prefixes', function (t) { }); api.on('test', function (a) { - var unnecessaryString = 'test' + separator + 'fixture' + separator; - index = expected.indexOf(a.title.replace(unnecessaryString, '')); + index = expected.indexOf(a.title); t.true(index >= 0); @@ -88,7 +89,27 @@ test('display filename prefixes for failed test stack traces', function (t) { .then(function () { t.is(api.passCount, 2); t.is(api.failCount, 1); - t.match(api.errors[0].title, /test \S fixture \S one-pass-one-fail \S this is a failing test/); + t.match(api.errors[0].title, /one-pass-one-fail \S this is a failing test/); + }); +}); + +// This is a seperate test because we can't ensure the order of the errors (to match them), and this is easier than +// sorting. +test('display filename prefixes for failed test stack traces in subdirs', function (t) { + t.plan(3); + + var files = [ + path.join(__dirname, 'fixture/es2015.js'), + path.join(__dirname, 'fixture/subdir/failing-subdir.js') + ]; + + var api = new Api(files); + + api.run() + .then(function () { + t.is(api.passCount, 1); + t.is(api.failCount, 1); + t.match(api.errors[0].title, /subdir \S failing-subdir \S subdir fail/); }); }); diff --git a/test/fixture/subdir/failing-subdir.js b/test/fixture/subdir/failing-subdir.js new file mode 100644 index 000000000..3ec477e94 --- /dev/null +++ b/test/fixture/subdir/failing-subdir.js @@ -0,0 +1,8 @@ +var test = require('../../../'); + +test('subdir fail', function (t) { + t.fail(); +}); + + + diff --git a/test/fixture/subdir/in-a-subdir.js b/test/fixture/subdir/in-a-subdir.js new file mode 100644 index 000000000..7e35907cf --- /dev/null +++ b/test/fixture/subdir/in-a-subdir.js @@ -0,0 +1,5 @@ +var test = require('../../../'); + +test('subdir', function (t) { + t.pass(); +}); From c56ed9e6463459ef3962b7b95e7ebb4980a2684c Mon Sep 17 00:00:00 2001 From: Ari Porad Date: Sat, 26 Dec 2015 11:02:19 -0800 Subject: [PATCH 3/3] Cleanup some stuff --- api.js | 22 ++-------------------- package.json | 1 + test/fixture/subdir/failing-subdir.js | 7 ++----- test/fixture/subdir/in-a-subdir.js | 4 ++-- test/fixture/subdir/nested/nested.js | 5 +++++ 5 files changed, 12 insertions(+), 27 deletions(-) create mode 100644 test/fixture/subdir/nested/nested.js diff --git a/api.js b/api.js index f0e93d921..1bef70b7b 100644 --- a/api.js +++ b/api.js @@ -8,6 +8,7 @@ var Promise = require('bluebird'); var figures = require('figures'); var globby = require('globby'); var chalk = require('chalk'); +var commondir = require('commondir'); var resolveCwd = require('resolve-cwd'); var fork = require('./lib/fork'); var formatter = require('./lib/enhance-assert').formatter(); @@ -100,25 +101,6 @@ Api.prototype._handleTest = function (test) { this.emit('test', test); }; -Api.prototype._findBase = function (files) { - this.base = files.reduce(function (base, file) { - file = path.relative('.', file); - file = file.split(path.sep); - if (base === false) { - return file; - } - return base.filter(function (part, i) { - return file[i].toLowerCase() === part.toLowerCase(); - }); - }, false).join(path.sep); - - if (this.base === '' || this.base === '.') { - this.base = this.files[0] || 'test'; - } - - this.base += path.sep; -}; - Api.prototype._prefixTitle = function (file) { if (this.fileCount === 1) { return ''; @@ -156,7 +138,7 @@ Api.prototype.run = function () { self.fileCount = files.length; - self._findBase(files); + self.base = path.relative('.', commondir('.', files)) + path.sep; var tests = files.map(self._runFile); diff --git a/package.json b/package.json index 824043e55..92a83ff1a 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ "cacha": "^1.0.3", "chalk": "^1.0.0", "co-with-promise": "^4.6.0", + "commondir": "^1.0.1", "core-assert": "^0.1.0", "debug": "^2.2.0", "deeper": "^2.1.0", diff --git a/test/fixture/subdir/failing-subdir.js b/test/fixture/subdir/failing-subdir.js index 3ec477e94..351c571e7 100644 --- a/test/fixture/subdir/failing-subdir.js +++ b/test/fixture/subdir/failing-subdir.js @@ -1,8 +1,5 @@ -var test = require('../../../'); +import test from '../../../'; -test('subdir fail', function (t) { +test('subdir fail', t => { t.fail(); }); - - - diff --git a/test/fixture/subdir/in-a-subdir.js b/test/fixture/subdir/in-a-subdir.js index 7e35907cf..3022a6714 100644 --- a/test/fixture/subdir/in-a-subdir.js +++ b/test/fixture/subdir/in-a-subdir.js @@ -1,5 +1,5 @@ -var test = require('../../../'); +import test from '../../../'; -test('subdir', function (t) { +test('subdir', t => { t.pass(); }); diff --git a/test/fixture/subdir/nested/nested.js b/test/fixture/subdir/nested/nested.js new file mode 100644 index 000000000..88e68ff6b --- /dev/null +++ b/test/fixture/subdir/nested/nested.js @@ -0,0 +1,5 @@ +import test from '../../../../'; + +test('subdir', t => { + t.pass(); +});