Skip to content

Commit 838d995

Browse files
r0mflipljharb
authored andcommitted
[New] Add descriptive messages for skipped asserts
1 parent 861cf55 commit 838d995

File tree

4 files changed

+165
-4
lines changed

4 files changed

+165
-4
lines changed

lib/results.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ var nextTick = typeof setImmediate !== 'undefined'
1616
module.exports = Results;
1717
inherits(Results, EventEmitter);
1818

19+
function coalesceWhiteSpaces(str) {
20+
return String(str).replace(/\s+/g, ' ');
21+
}
22+
1923
function Results() {
2024
if (!(this instanceof Results)) return new Results;
2125
this.count = 0;
@@ -102,7 +106,7 @@ Results.prototype._watch = function (t) {
102106
var premsg = '';
103107
if (t._skip) premsg = 'SKIP ';
104108
else if (t._todo) premsg = 'TODO ';
105-
write('# ' + premsg + t.name + '\n');
109+
write('# ' + premsg + coalesceWhiteSpaces(t.name) + '\n');
106110
});
107111

108112
t.on('result', function (res) {
@@ -142,10 +146,13 @@ Results.prototype.close = function () {
142146
function encodeResult(res, count) {
143147
var output = '';
144148
output += (res.ok ? 'ok ' : 'not ok ') + count;
145-
output += res.name ? ' ' + res.name.toString().replace(/\s+/g, ' ') : '';
149+
output += res.name ? ' ' + coalesceWhiteSpaces(res.name) : '';
146150

147-
if (res.skip) output += ' # SKIP';
148-
else if (res.todo) output += ' # TODO';
151+
if (res.skip) {
152+
output += ' # SKIP' + ((typeof res.skip === 'string') ? ' ' + coalesceWhiteSpaces(res.skip) : '');
153+
} else if (res.todo) {
154+
output += ' # TODO' + ((typeof res.todo === 'string') ? ' ' + coalesceWhiteSpaces(res.todo) : '');
155+
};
149156

150157
output += '\n';
151158
if (res.ok) return output;

lib/test.js

+2
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ Test.prototype._assert = function assert(ok, opts) {
202202
var self = this;
203203
var extra = opts.extra || {};
204204

205+
ok = Boolean(ok) || Boolean(extra.skip);
206+
205207
var res = {
206208
id: self.assertCount++,
207209
ok: Boolean(ok),

test/skip_explanation.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var tap = require('tap');
2+
var test = require('../');
3+
var concat = require('concat-stream');
4+
var stripFullStack = require('./common').stripFullStack;
5+
6+
tap.test('test skip explanations', function (assert) {
7+
assert.plan(1);
8+
9+
var verify = function (output) {
10+
assert.equal(stripFullStack(output.toString('utf8')), [
11+
'TAP version 13',
12+
'# SKIP (this skips)',
13+
'# some tests might skip',
14+
'ok 1 this runs',
15+
'ok 2 failing assert is skipped # SKIP',
16+
'ok 3 this runs',
17+
'# incomplete test',
18+
'ok 4 run sh',
19+
'ok 5 run openssl # SKIP',
20+
'# incomplete test with explanation',
21+
'ok 6 run sh (conditional skip) # SKIP',
22+
'ok 7 run openssl # SKIP can\'t run on windows platforms',
23+
'ok 8 this runs',
24+
'# too much explanation',
25+
'ok 9 run openssl # SKIP Installer cannot work on windows and fails to add to PATH Err: (2401) denied',
26+
'',
27+
'1..9',
28+
'# tests 9',
29+
'# pass 9',
30+
'',
31+
'# ok',
32+
''
33+
].join('\n'));
34+
};
35+
36+
var tapeTest = test.createHarness();
37+
tapeTest.createStream().pipe(concat(verify));
38+
39+
tapeTest('(this skips)', { skip: true }, function (t) {
40+
t.fail('doesn\'t run');
41+
t.fail('this doesn\'t run too', { skip: false });
42+
t.end();
43+
});
44+
45+
tapeTest('some tests might skip', function (t) {
46+
t.pass('this runs');
47+
t.fail('failing assert is skipped', { skip: true });
48+
t.pass('this runs');
49+
t.end();
50+
});
51+
52+
tapeTest('incomplete test', function (t) {
53+
// var platform = process.platform; something like this needed
54+
var platform = 'win32';
55+
56+
t.pass('run sh', { skip: platform !== 'win32' });
57+
t.pass('run openssl', { skip: platform === 'win32' });
58+
t.end();
59+
});
60+
61+
tapeTest('incomplete test with explanation', function (t) {
62+
// var platform = process.platform; something like this needed
63+
var platform = 'win32';
64+
65+
t.fail('run sh (conditional skip)', { skip: platform === 'win32' });
66+
t.fail('run openssl', { skip: platform === 'win32' && 'can\'t run on windows platforms' });
67+
t.pass('this runs');
68+
t.end();
69+
});
70+
71+
tapeTest('too much explanation', function (t) {
72+
// var platform = process.platform; something like this needed
73+
var platform = 'win32';
74+
75+
t.fail('run openssl',
76+
{ skip: platform === 'win32' && 'Installer cannot work on windows\nand fails to add to PATH\n\n Err: (2401) denied' }
77+
);
78+
t.end();
79+
});
80+
});
81+
82+
// vim: set softtabstop=4 shiftwidth=4:

test/todo_explanation.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
var tap = require('tap');
2+
var tape = require('../');
3+
var concat = require('concat-stream');
4+
5+
var common = require('./common');
6+
var stripFullStack = common.stripFullStack;
7+
8+
tap.test('tape todo test', { todo: process.versions.node.match(/0\.8\.\d+/) ? 'Fails on node 0.8': false }, function (assert) {
9+
var test = tape.createHarness({ exit: false });
10+
assert.plan(1);
11+
12+
test.createStream().pipe(concat(function (body) {
13+
assert.equal(
14+
stripFullStack(body.toString('utf8')), [
15+
'TAP version 13',
16+
'# success',
17+
'ok 1 this test runs',
18+
'# TODO incomplete test1',
19+
'not ok 2 check output # TODO',
20+
' ---',
21+
' operator: equal',
22+
' expected: false',
23+
' actual: true',
24+
' at: Test.<anonymous> ($TEST/todo_explanation.js:$LINE:$COL)',
25+
' ...',
26+
'not ok 3 check vars output # TODO name conflict',
27+
' ---',
28+
' operator: equal',
29+
' expected: 0',
30+
' actual: 1',
31+
' at: Test.<anonymous> ($TEST/todo_explanation.js:$LINE:$COL)',
32+
' ...',
33+
'# incomplete test2',
34+
'not ok 4 run openssl # TODO installer needs fix',
35+
' ---',
36+
' operator: fail',
37+
' at: Test.<anonymous> ($TEST/todo_explanation.js:$LINE:$COL)',
38+
' ...',
39+
'# TODO passing test',
40+
'',
41+
'1..4',
42+
'# tests 4',
43+
'# pass 4',
44+
'',
45+
'# ok',
46+
''
47+
].join('\n')
48+
);
49+
}));
50+
51+
test('success', function (t) {
52+
t.equal(true, true, 'this test runs');
53+
t.end();
54+
});
55+
56+
test('incomplete test1', { todo: true }, function (t) {
57+
t.equal(true, false, 'check output');
58+
t.equal(1, 0, 'check vars output', { todo: 'name conflict' });
59+
t.end();
60+
});
61+
62+
test('incomplete test2', function (t) {
63+
t.fail('run openssl', { todo: 'installer needs fix' });
64+
t.end();
65+
});
66+
67+
test('passing test', { todo: 'yet incomplete' }, function (t) {
68+
t.end();
69+
});
70+
});

0 commit comments

Comments
 (0)