Skip to content

Commit 2d98da9

Browse files
committed
Add support for test comments.
There are times when you wish to include comments in your tests that will be shown in context of the test result and not streamed to stdout like console.log statements are.
1 parent a1afbe3 commit 2d98da9

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

lib/assert.js

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ function wrapAssertions(callbacks) {
6464
const pass = callbacks.pass;
6565
const pending = callbacks.pending;
6666
const fail = callbacks.fail;
67+
const comment = callbacks.comment;
6768

6869
const noop = () => {};
6970
const makeRethrow = reason => () => {
@@ -110,6 +111,10 @@ function wrapAssertions(callbacks) {
110111
}
111112
},
112113

114+
comment(text) {
115+
comment(this, text);
116+
},
117+
113118
deepEqual(actual, expected, message) {
114119
const result = concordance.compare(actual, expected, concordanceOptions);
115120
if (result.pass) {

lib/colors.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const chalk = require('chalk');
33

44
module.exports = {
5+
comment: chalk.gray,
56
title: chalk.bold.white,
67
error: chalk.red,
78
skip: chalk.yellow,

lib/reporters/tap.js

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class TapReporter {
7575
];
7676
}
7777

78+
if (test.comments) {
79+
test.comments.forEach(comment => {
80+
output.push(` ${comment}`);
81+
});
82+
}
83+
7884
return output.join('\n');
7985
}
8086
unhandledError(err) {

lib/reporters/verbose.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,32 @@ class VerboseReporter {
2424
return '';
2525
}
2626
test(test, runStatus) {
27+
const lines = [];
2728
if (test.error) {
28-
return ' ' + colors.error(figures.cross) + ' ' + test.title + ' ' + colors.error(test.error.message);
29-
}
30-
31-
if (test.todo) {
32-
return ' ' + colors.todo('- ' + test.title);
29+
lines.push(' ' + colors.error(figures.cross) + ' ' + test.title + ' ' + colors.error(test.error.message));
30+
} else if (test.todo) {
31+
lines.push(' ' + colors.todo('- ' + test.title));
3332
} else if (test.skip) {
34-
return ' ' + colors.skip('- ' + test.title);
35-
}
33+
lines.push(' ' + colors.skip('- ' + test.title));
34+
} else if (test.failing) {
35+
lines.push(' ' + colors.error(figures.tick) + ' ' + colors.error(test.title));
36+
} else if (runStatus.fileCount === 1 && runStatus.testCount === 1 && test.title === '[anonymous]') {
37+
// No output
38+
} else {
39+
// Display duration only over a threshold
40+
const threshold = 100;
41+
const duration = test.duration > threshold ? colors.duration(' (' + prettyMs(test.duration) + ')') : '';
3642

37-
if (test.failing) {
38-
return ' ' + colors.error(figures.tick) + ' ' + colors.error(test.title);
43+
lines.push(' ' + colors.pass(figures.tick) + ' ' + test.title + duration);
3944
}
4045

41-
if (runStatus.fileCount === 1 && runStatus.testCount === 1 && test.title === '[anonymous]') {
42-
return undefined;
46+
if (test.comments) {
47+
test.comments.forEach(comment => {
48+
lines.push(' ' + colors.information(figures.info) + ' ' + colors.comment(comment));
49+
});
4350
}
4451

45-
// Display duration only over a threshold
46-
const threshold = 100;
47-
const duration = test.duration > threshold ? colors.duration(' (' + prettyMs(test.duration) + ')') : '';
48-
49-
return ' ' + colors.pass(figures.tick) + ' ' + test.title + duration;
52+
return lines.length > 0 ? lines.join('\n') : undefined;
5053
}
5154
unhandledError(err) {
5255
if (err.type === 'exception' && err.name === 'AvaError') {

lib/runner.js

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class Runner extends EventEmitter {
130130
addTestResult(result) {
131131
const test = result.result;
132132
const props = {
133+
comments: test.comments,
133134
duration: test.duration,
134135
title: test.title,
135136
error: result.reason,

lib/test.js

+9
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class ExecutionContext {
7878

7979
{
8080
const assertions = assert.wrapAssertions({
81+
comment(executionContext, text) {
82+
executionContext._test.addComment(text);
83+
},
84+
8185
pass(executionContext) {
8286
executionContext._test.countPassedAssertion();
8387
},
@@ -108,6 +112,7 @@ class Test {
108112
this.metadata = options.metadata;
109113
this.onResult = options.onResult;
110114
this.title = options.title;
115+
this.comments = [];
111116

112117
this.snapshotInvocationCount = 0;
113118
this.compareWithSnapshot = assertionOptions => {
@@ -175,6 +180,10 @@ class Test {
175180
this.assertCount++;
176181
}
177182

183+
addComment(text) {
184+
this.comments.push(text);
185+
}
186+
178187
addPendingAssertion(promise) {
179188
if (this.finishing) {
180189
this.saveFirstError(new Error('Assertion passed, but test has already finished'));

0 commit comments

Comments
 (0)