Skip to content

Commit

Permalink
Better test prefixes
Browse files Browse the repository at this point in the history
closes #379
  • Loading branch information
ariporad authored and sindresorhus committed Dec 28, 2015
1 parent 5aeb270 commit fc3b969
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
15 changes: 6 additions & 9 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 AvaError = require('./lib/ava-error');
var fork = require('./lib/fork');
Expand All @@ -32,6 +33,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);
Expand Down Expand Up @@ -107,17 +109,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)
Expand All @@ -144,6 +139,8 @@ Api.prototype.run = function () {

self.fileCount = files.length;

self.base = path.relative('.', commondir('.', files)) + path.sep;

var tests = files.map(self._runFile);

// receive test count from all files and then run the tests
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 27 additions & 6 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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/);
});
});

Expand Down
5 changes: 5 additions & 0 deletions test/fixture/subdir/failing-subdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../';

test('subdir fail', t => {
t.fail();
});
5 changes: 5 additions & 0 deletions test/fixture/subdir/in-a-subdir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../';

test('subdir', t => {
t.pass();
});
5 changes: 5 additions & 0 deletions test/fixture/subdir/nested/nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import test from '../../../../';

test('subdir', t => {
t.pass();
});

0 comments on commit fc3b969

Please sign in to comment.