Skip to content

Commit 02aa7a2

Browse files
committed
PR feedback.
Added indentString support. Fixed up readme.
1 parent d14e4d8 commit 02aa7a2

File tree

7 files changed

+97
-14
lines changed

7 files changed

+97
-14
lines changed

lib/reporters/mini.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,13 @@ class MiniReporter {
167167

168168
if (test.logs) {
169169
test.logs.forEach(log => {
170-
status += ' ' + colors.information(figures.info) + ' ' + colors.log(log) + '\n';
170+
const logLines = indentString(colors.log(log), 6);
171+
const logLinesWithLeadingFigure = logLines.replace(
172+
/^ {6}/,
173+
` ${colors.information(figures.info)} `
174+
);
175+
176+
status += logLinesWithLeadingFigure + '\n';
171177
});
172178

173179
status += '\n';

lib/reporters/tap.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ class TapReporter {
6565
const appendLogs = () => {
6666
if (test.logs) {
6767
test.logs.forEach(log => {
68-
output.push(` ${log}`);
68+
const logLines = indentString(log, 4);
69+
const logLinesWithLeadingFigure = logLines.replace(
70+
/^ {4}/,
71+
' * '
72+
);
73+
74+
output.push(logLinesWithLeadingFigure);
6975
});
7076
}
7177
};

lib/reporters/verbose.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ class VerboseReporter {
4545

4646
if (test.logs) {
4747
test.logs.forEach(log => {
48-
lines.push(' ' + colors.information(figures.info) + ' ' + colors.log(log));
48+
const logLines = indentString(colors.log(log), 6);
49+
const logLinesWithLeadingFigure = logLines.replace(
50+
/^ {6}/,
51+
` ${colors.information(figures.info)} `
52+
);
53+
54+
lines.push(logLinesWithLeadingFigure);
4955
});
5056
}
5157

@@ -110,7 +116,13 @@ class VerboseReporter {
110116

111117
if (test.logs) {
112118
test.logs.forEach(log => {
113-
output += ' ' + colors.information(figures.info) + ' ' + colors.log(log) + '\n';
119+
const logLines = indentString(colors.log(log), 6);
120+
const logLinesWithLeadingFigure = logLines.replace(
121+
/^ {6}/,
122+
` ${colors.information(figures.info)} `
123+
);
124+
125+
output += logLinesWithLeadingFigure + '\n';
114126
});
115127

116128
output += '\n';

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ End the test. Only works with `test.cb()`.
877877

878878
###### `t.log(message)`
879879

880-
Print a log message contextually alongside the test result instead of immediately streaming the message to stdout like `console.log`.
880+
Print a log message contextually alongside the test result instead of immediately printing it to `stdout` like `console.log`.
881881

882882
## Assertions
883883

test/reporters/mini.js

+54
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const indentString = require('indent-string');
33
const tempWrite = require('temp-write');
44
const flatten = require('arr-flatten');
5+
const figures = require('figures');
56
const chalk = require('chalk');
67
const sinon = require('sinon');
78
const test = require('tap').test;
@@ -934,3 +935,56 @@ test('result when no-color flag is set', t => {
934935
t.is(output, expectedOutput);
935936
t.end();
936937
});
938+
939+
test('results with errors and logs', t => {
940+
const err1 = new Error('failure one');
941+
err1.stack = beautifyStack(err1.stack);
942+
const err1Path = tempWrite.sync('a();');
943+
err1.source = source(err1Path);
944+
err1.avaAssertionError = true;
945+
err1.statements = [];
946+
err1.values = [
947+
{label: 'actual:', formatted: JSON.stringify('abc') + '\n'},
948+
{label: 'expected:', formatted: JSON.stringify('abd') + '\n'}
949+
];
950+
951+
const reporter = miniReporter();
952+
reporter.failCount = 1;
953+
954+
const runStatus = {
955+
errors: [{
956+
title: 'failed one',
957+
logs: ['log from a failed test\nwith a newline', 'another log from failed test'],
958+
error: err1
959+
}]
960+
};
961+
962+
const output = reporter.finish(runStatus);
963+
compareLineOutput(t, output, flatten([
964+
'',
965+
' ' + chalk.red('1 failed'),
966+
'',
967+
' ' + chalk.bold.white('failed one'),
968+
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log from a failed test'),
969+
' ' + chalk.gray('with a newline'),
970+
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('another log from failed test'),
971+
'',
972+
' ' + chalk.grey(`${err1.source.file}:${err1.source.line}`),
973+
'',
974+
indentString(codeExcerpt(err1.source), 2).split('\n'),
975+
'',
976+
/failure one/,
977+
'',
978+
' actual:',
979+
'',
980+
' "abc"',
981+
'',
982+
' expected:',
983+
'',
984+
' "abd"',
985+
'',
986+
stackLineRegex, compareLineOutput.SKIP_UNTIL_EMPTY_LINE,
987+
''
988+
]));
989+
t.end();
990+
});

test/reporters/tap.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,15 @@ test('successful test with logs', t => {
272272

273273
const actualOutput = reporter.test({
274274
title: 'passing',
275-
logs: ['log message 1', 'log message 2']
275+
logs: ['log message 1\nwith a newline', 'log message 2']
276276
});
277277

278278
const expectedOutput = [
279279
'# passing',
280280
'ok 1 - passing',
281-
' log message 1',
282-
' log message 2'
281+
' * log message 1',
282+
' with a newline',
283+
' * log message 2'
283284
].join('\n');
284285

285286
t.is(actualOutput, expectedOutput);
@@ -295,14 +296,15 @@ test('failing test with logs', t => {
295296
name: 'AssertionError',
296297
message: 'false == true'
297298
},
298-
logs: ['log message 1', 'log message 2']
299+
logs: ['log message 1\nwith a newline', 'log message 2']
299300
});
300301

301302
const expectedOutput = [
302303
'# failing',
303304
'not ok 1 - failing',
304-
' log message 1',
305-
' log message 2',
305+
' * log message 1',
306+
' with a newline',
307+
' * log message 2',
306308
' ---',
307309
' name: AssertionError',
308310
' message: false == true',

test/reporters/verbose.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -853,12 +853,13 @@ test('successful test with logs', t => {
853853

854854
const actualOutput = reporter.test({
855855
title: 'successful test',
856-
logs: ['log message 1', 'log message 2']
856+
logs: ['log message 1\nwith a newline', 'log message 2']
857857
}, {});
858858

859859
const expectedOutput = [
860860
' ' + chalk.green(figures.tick) + ' successful test',
861861
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log message 1'),
862+
' ' + chalk.gray('with a newline'),
862863
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log message 2')
863864
].join('\n');
864865

@@ -872,12 +873,13 @@ test('failed test with logs', t => {
872873
const actualOutput = reporter.test({
873874
title: 'failed test',
874875
error: new Error('failure'),
875-
logs: ['log message 1', 'log message 2']
876+
logs: ['log message 1\nwith a newline', 'log message 2']
876877
}, {});
877878

878879
const expectedOutput = [
879880
' ' + chalk.red(figures.cross) + ' failed test ' + chalk.red('failure'),
880881
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log message 1'),
882+
' ' + chalk.gray('with a newline'),
881883
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log message 2')
882884
].join('\n');
883885

@@ -902,7 +904,7 @@ test('results with errors and logs', t => {
902904
runStatus.failCount = 1;
903905
runStatus.tests = [{
904906
title: 'fail one',
905-
logs: ['log from failed test', 'another log from failed test'],
907+
logs: ['log from failed test\nwith a newline', 'another log from failed test'],
906908
error: error1
907909
}];
908910

@@ -913,6 +915,7 @@ test('results with errors and logs', t => {
913915
'',
914916
' ' + chalk.bold.white('fail one'),
915917
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('log from failed test'),
918+
' ' + chalk.gray('with a newline'),
916919
' ' + chalk.magenta(figures.info) + ' ' + chalk.gray('another log from failed test'),
917920
'',
918921
' ' + chalk.grey(`${error1.source.file}:${error1.source.line}`),

0 commit comments

Comments
 (0)