Skip to content

Commit fc3b969

Browse files
ariporadsindresorhus
authored andcommitted
Better test prefixes
closes #379
1 parent 5aeb270 commit fc3b969

File tree

6 files changed

+49
-15
lines changed

6 files changed

+49
-15
lines changed

api.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Promise = require('bluebird');
88
var figures = require('figures');
99
var globby = require('globby');
1010
var chalk = require('chalk');
11+
var commondir = require('commondir');
1112
var resolveCwd = require('resolve-cwd');
1213
var AvaError = require('./lib/ava-error');
1314
var fork = require('./lib/fork');
@@ -32,6 +33,7 @@ function Api(files, options) {
3233
this.stats = [];
3334
this.tests = [];
3435
this.files = files || [];
36+
this.base = '';
3537

3638
Object.keys(Api.prototype).forEach(function (key) {
3739
this[key] = this[key].bind(this);
@@ -107,17 +109,10 @@ Api.prototype._prefixTitle = function (file) {
107109

108110
var separator = ' ' + chalk.gray.dim(figures.pointerSmall) + ' ';
109111

110-
var base = path.dirname(this.files[0]);
111-
112-
if (base === '.') {
113-
base = this.files[0] || 'test';
114-
}
115-
116-
base += path.sep;
117-
118112
var prefix = path.relative('.', file)
119-
.replace(base, '')
113+
.replace(this.base, '')
120114
.replace(/\.spec/, '')
115+
.replace(/\.test/, '')
121116
.replace(/test\-/g, '')
122117
.replace(/\.js$/, '')
123118
.split(path.sep)
@@ -144,6 +139,8 @@ Api.prototype.run = function () {
144139

145140
self.fileCount = files.length;
146141

142+
self.base = path.relative('.', commondir('.', files)) + path.sep;
143+
147144
var tests = files.map(self._runFile);
148145

149146
// receive test count from all files and then run the tests

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"cacha": "^1.0.3",
9090
"chalk": "^1.0.0",
9191
"co-with-promise": "^4.6.0",
92+
"commondir": "^1.0.1",
9293
"core-assert": "^0.1.0",
9394
"debug": "^2.2.0",
9495
"deeper": "^2.1.0",

test/api.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,21 @@ test('async/await support', function (t) {
3838
});
3939

4040
test('test title prefixes', function (t) {
41-
t.plan(5);
41+
t.plan(6);
4242

4343
var separator = ' ' + figures.pointerSmall + ' ';
4444
var files = [
4545
path.join(__dirname, 'fixture/async-await.js'),
4646
path.join(__dirname, 'fixture/es2015.js'),
47-
path.join(__dirname, 'fixture/generators.js')
47+
path.join(__dirname, 'fixture/generators.js'),
48+
path.join(__dirname, 'fixture/subdir/in-a-subdir.js')
4849
];
4950
var expected = [
5051
['async-await', 'async function'].join(separator),
5152
['async-await', 'arrow async function'].join(separator),
5253
['es2015', '[anonymous]'].join(separator),
53-
['generators', 'generator function'].join(separator)
54+
['generators', 'generator function'].join(separator),
55+
['subdir', 'in-a-subdir', 'subdir'].join(separator)
5456
];
5557
var index;
5658

@@ -64,8 +66,7 @@ test('test title prefixes', function (t) {
6466
});
6567

6668
api.on('test', function (a) {
67-
var unnecessaryString = 'test' + separator + 'fixture' + separator;
68-
index = expected.indexOf(a.title.replace(unnecessaryString, ''));
69+
index = expected.indexOf(a.title);
6970

7071
t.true(index >= 0);
7172

@@ -88,7 +89,27 @@ test('display filename prefixes for failed test stack traces', function (t) {
8889
.then(function () {
8990
t.is(api.passCount, 2);
9091
t.is(api.failCount, 1);
91-
t.match(api.errors[0].title, /test \S fixture \S one-pass-one-fail \S this is a failing test/);
92+
t.match(api.errors[0].title, /one-pass-one-fail \S this is a failing test/);
93+
});
94+
});
95+
96+
// This is a seperate test because we can't ensure the order of the errors (to match them), and this is easier than
97+
// sorting.
98+
test('display filename prefixes for failed test stack traces in subdirs', function (t) {
99+
t.plan(3);
100+
101+
var files = [
102+
path.join(__dirname, 'fixture/es2015.js'),
103+
path.join(__dirname, 'fixture/subdir/failing-subdir.js')
104+
];
105+
106+
var api = new Api(files);
107+
108+
api.run()
109+
.then(function () {
110+
t.is(api.passCount, 1);
111+
t.is(api.failCount, 1);
112+
t.match(api.errors[0].title, /subdir \S failing-subdir \S subdir fail/);
92113
});
93114
});
94115

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../../';
2+
3+
test('subdir fail', t => {
4+
t.fail();
5+
});

test/fixture/subdir/in-a-subdir.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../../';
2+
3+
test('subdir', t => {
4+
t.pass();
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import test from '../../../../';
2+
3+
test('subdir', t => {
4+
t.pass();
5+
});

0 commit comments

Comments
 (0)