Skip to content

Commit 56344c3

Browse files
committed
Added PR review comments.
* [x] Switched from `t.comment` to `t.log` * [x] Added mini reporter comments on failure. * [x] Added tests.
1 parent a69aac7 commit 56344c3

File tree

10 files changed

+200
-29
lines changed

10 files changed

+200
-29
lines changed

lib/assert.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +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;
67+
const log = callbacks.log;
6868

6969
const noop = () => {};
7070
const makeRethrow = reason => () => {
@@ -111,8 +111,8 @@ function wrapAssertions(callbacks) {
111111
}
112112
},
113113

114-
comment(text) {
115-
comment(this, text);
114+
log(text) {
115+
log(this, text);
116116
},
117117

118118
deepEqual(actual, expected, message) {

lib/colors.js

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

44
module.exports = {
5-
comment: chalk.gray,
5+
log: chalk.gray,
66
title: chalk.bold.white,
77
error: chalk.red,
88
skip: chalk.yellow,

lib/reporters/mini.js

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const lastLineTracker = require('last-line-stream/tracker');
55
const plur = require('plur');
66
const spinners = require('cli-spinners');
77
const chalk = require('chalk');
8+
const figures = require('figures');
89
const cliTruncate = require('cli-truncate');
910
const cross = require('figures').cross;
1011
const indentString = require('indent-string');
@@ -163,6 +164,15 @@ class MiniReporter {
163164
}
164165

165166
status += ' ' + colors.title(test.title) + '\n';
167+
168+
if (test.logs) {
169+
test.logs.forEach(log => {
170+
status += ' ' + colors.information(figures.info) + ' ' + colors.log(log) + '\n';
171+
});
172+
173+
status += '\n';
174+
}
175+
166176
if (test.error.source) {
167177
status += ' ' + colors.errorSource(test.error.source.file + ':' + test.error.source.line) + '\n';
168178

lib/reporters/tap.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TapReporter {
4949
return 'TAP version 13';
5050
}
5151
test(test) {
52-
let output;
52+
const output = [];
5353

5454
let directive = '';
5555
const passed = test.todo ? 'not ok' : 'ok';
@@ -62,23 +62,23 @@ class TapReporter {
6262

6363
const title = stripAnsi(test.title);
6464

65+
const appendLogs = () => {
66+
if (test.logs) {
67+
test.logs.forEach(log => {
68+
output.push(` ${log}`);
69+
});
70+
}
71+
};
72+
73+
output.push(`# ${title}`);
74+
6575
if (test.error) {
66-
output = [
67-
'# ' + title,
68-
format('not ok %d - %s', ++this.i, title),
69-
dumpError(test.error, true)
70-
];
76+
output.push(format('not ok %d - %s', ++this.i, title));
77+
appendLogs();
78+
output.push(dumpError(test.error, true));
7179
} else {
72-
output = [
73-
`# ${title}`,
74-
format('%s %d - %s %s', passed, ++this.i, title, directive).trim()
75-
];
76-
}
77-
78-
if (test.comments) {
79-
test.comments.forEach(comment => {
80-
output.push(` ${comment}`);
81-
});
80+
output.push(format('%s %d - %s %s', passed, ++this.i, title, directive).trim());
81+
appendLogs();
8282
}
8383

8484
return output.join('\n');

lib/reporters/verbose.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ class VerboseReporter {
4343
lines.push(' ' + colors.pass(figures.tick) + ' ' + test.title + duration);
4444
}
4545

46-
if (test.comments) {
47-
test.comments.forEach(comment => {
48-
lines.push(' ' + colors.information(figures.info) + ' ' + colors.comment(comment));
46+
if (test.logs) {
47+
test.logs.forEach(log => {
48+
lines.push(' ' + colors.information(figures.info) + ' ' + colors.log(log));
4949
});
5050
}
5151

@@ -107,6 +107,15 @@ class VerboseReporter {
107107
}
108108

109109
output += ' ' + colors.title(test.title) + '\n';
110+
111+
if (test.logs) {
112+
test.logs.forEach(log => {
113+
output += ' ' + colors.information(figures.info) + ' ' + colors.log(log) + '\n';
114+
});
115+
116+
output += '\n';
117+
}
118+
110119
if (test.error.source) {
111120
output += ' ' + colors.errorSource(test.error.source.file + ':' + test.error.source.line) + '\n';
112121

lib/runner.js

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

lib/test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class ExecutionContext {
7878

7979
{
8080
const assertions = assert.wrapAssertions({
81-
comment(executionContext, text) {
82-
executionContext._test.addComment(text);
81+
log(executionContext, text) {
82+
executionContext._test.addLog(text);
8383
},
8484

8585
pass(executionContext) {
@@ -112,7 +112,7 @@ class Test {
112112
this.metadata = options.metadata;
113113
this.onResult = options.onResult;
114114
this.title = options.title;
115-
this.comments = [];
115+
this.logs = [];
116116

117117
this.snapshotInvocationCount = 0;
118118
this.compareWithSnapshot = assertionOptions => {
@@ -180,8 +180,8 @@ class Test {
180180
this.assertCount++;
181181
}
182182

183-
addComment(text) {
184-
this.comments.push(text);
183+
addLog(text) {
184+
this.logs.push(text);
185185
}
186186

187187
addPendingAssertion(promise) {

test/reporters/tap.js

+46
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,49 @@ test('stdout and stderr should call process.stderr.write', t => {
266266
t.is(stub.callCount, 2);
267267
t.end();
268268
});
269+
270+
test('successful test with logs', t => {
271+
const reporter = new TapReporter();
272+
273+
const actualOutput = reporter.test({
274+
title: 'passing',
275+
logs: ['log message 1', 'log message 2']
276+
});
277+
278+
const expectedOutput = [
279+
'# passing',
280+
'ok 1 - passing',
281+
' log message 1',
282+
' log message 2'
283+
].join('\n');
284+
285+
t.is(actualOutput, expectedOutput);
286+
t.end();
287+
});
288+
289+
test('failing test with logs', t => {
290+
const reporter = new TapReporter();
291+
292+
const actualOutput = reporter.test({
293+
title: 'failing',
294+
error: {
295+
name: 'AssertionError',
296+
message: 'false == true'
297+
},
298+
logs: ['log message 1', 'log message 2']
299+
});
300+
301+
const expectedOutput = [
302+
'# failing',
303+
'not ok 1 - failing',
304+
' log message 1',
305+
' log message 2',
306+
' ---',
307+
' name: AssertionError',
308+
' message: false == true',
309+
' ...'
310+
].join('\n');
311+
312+
t.is(actualOutput, expectedOutput);
313+
t.end();
314+
});

test/reporters/verbose.js

+87
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,90 @@ test('result when no-color flag is set', t => {
847847
t.is(output, expectedOutput);
848848
t.end();
849849
});
850+
851+
test('successful test with logs', t => {
852+
const reporter = createReporter();
853+
854+
const actualOutput = reporter.test({
855+
title: 'successful test',
856+
logs: ['log message 1', 'log message 2']
857+
}, {});
858+
859+
const expectedOutput = [
860+
' ' + chalk.green(figures.tick) + ' successful test',
861+
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log message 1'),
862+
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log message 2')
863+
].join('\n');
864+
865+
t.is(actualOutput, expectedOutput);
866+
t.end();
867+
});
868+
869+
test('failed test with logs', t => {
870+
const reporter = createReporter();
871+
872+
const actualOutput = reporter.test({
873+
title: 'failed test',
874+
error: new Error('failure'),
875+
logs: ['log message 1', 'log message 2']
876+
}, {});
877+
878+
const expectedOutput = [
879+
' ' + chalk.red(figures.cross) + ' failed test ' + chalk.red('failure'),
880+
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log message 1'),
881+
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log message 2')
882+
].join('\n');
883+
884+
t.is(actualOutput, expectedOutput);
885+
t.end();
886+
});
887+
888+
test('results with errors and logs', t => {
889+
const error1 = new Error('error one message');
890+
error1.stack = beautifyStack(error1.stack);
891+
const err1Path = tempWrite.sync('a()');
892+
error1.source = source(err1Path);
893+
error1.avaAssertionError = true;
894+
error1.statements = [];
895+
error1.values = [
896+
{label: 'actual:', formatted: JSON.stringify('abc')},
897+
{label: 'expected:', formatted: JSON.stringify('abd')}
898+
];
899+
900+
const reporter = createReporter({color: true});
901+
const runStatus = createRunStatus();
902+
runStatus.failCount = 1;
903+
runStatus.tests = [{
904+
title: 'fail one',
905+
logs: ['log from failed test', 'another log from failed test'],
906+
error: error1
907+
}];
908+
909+
const output = reporter.finish(runStatus);
910+
compareLineOutput(t, output, flatten([
911+
'',
912+
' ' + chalk.red('1 test failed') + time,
913+
'',
914+
' ' + chalk.bold.white('fail one'),
915+
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log from failed test'),
916+
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('another log from failed test'),
917+
'',
918+
' ' + chalk.grey(`${error1.source.file}:${error1.source.line}`),
919+
'',
920+
indentString(codeExcerpt(error1.source), 2).split('\n'),
921+
'',
922+
/error one message/,
923+
'',
924+
' actual:',
925+
'',
926+
' "abc"',
927+
'',
928+
' expected:',
929+
'',
930+
' "abd"',
931+
'',
932+
stackLineRegex, compareLineOutput.SKIP_UNTIL_EMPTY_LINE,
933+
''
934+
]));
935+
t.end();
936+
});

test/test.js

+19
Original file line numberDiff line numberDiff line change
@@ -733,3 +733,22 @@ test('failing tests fail with `t.notThrows(throws)`', t => {
733733
t.end();
734734
});
735735
});
736+
737+
test('log from tests', t => {
738+
let result;
739+
740+
ava(a => {
741+
a.log('a log message from a test');
742+
t.true(true);
743+
a.log('another log message from a test');
744+
}, null, r => {
745+
result = r;
746+
}).run();
747+
748+
t.deepEqual(
749+
result.result.logs,
750+
['a log message from a test', 'another log message from a test']
751+
);
752+
753+
t.end();
754+
});

0 commit comments

Comments
 (0)