Skip to content

Commit 8628976

Browse files
yovasx2novemberborn
authored andcommitted
Print hooks and remaining tests as TAP comments
Fixes #2142.
1 parent f01d05f commit 8628976

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

lib/reporters/tap.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path');
55
const plur = require('plur');
66
const stripAnsi = require('strip-ansi');
77
const supertap = require('supertap');
8+
const indentString = require('indent-string');
89

910
const prefixTitle = require('./prefix-title');
1011

@@ -74,7 +75,7 @@ class TapReporter {
7475
if (this.stats) {
7576
this.reportStream.write(supertap.finish({
7677
crashed: this.crashCount,
77-
failed: this.stats.failedHooks + this.stats.failedTests,
78+
failed: this.stats.failedTests + this.stats.remainingTests,
7879
passed: this.stats.passedTests + this.stats.passedKnownFailingTests,
7980
skipped: this.stats.skippedTests,
8081
todo: this.stats.todoTests
@@ -118,6 +119,21 @@ class TapReporter {
118119
}) + os.EOL);
119120
}
120121

122+
writeComment(evt, {error = false, title = this.prefixTitle(evt.testFile, evt.title)}) {
123+
let formattedTitle = title;
124+
if (error) {
125+
formattedTitle = `Failed hook: ${formattedTitle}`;
126+
}
127+
128+
this.reportStream.write(`# ${stripAnsi(formattedTitle)}${os.EOL}`);
129+
if (evt.logs) {
130+
for (const log of evt.logs) {
131+
const logLines = indentString(log, 4).replace(/^ {4}/, ' # ');
132+
this.reportStream.write(`${logLines}${os.EOL}`);
133+
}
134+
}
135+
}
136+
121137
consumeStateChange(evt) { // eslint-disable-line complexity
122138
const fileStats = this.stats && evt.testFile ? this.stats.byFile.get(evt.testFile) : null;
123139

@@ -126,10 +142,10 @@ class TapReporter {
126142
// Ignore
127143
break;
128144
case 'hook-failed':
129-
this.writeTest(evt, {passed: false, todo: false, skip: false});
145+
this.writeComment(evt, {error: true});
130146
break;
131147
case 'hook-finished':
132-
this.writeTest(evt, {passed: true, todo: false, skip: false});
148+
this.writeComment(evt, {});
133149
break;
134150
case 'internal-error':
135151
this.writeCrash(evt);
@@ -179,7 +195,7 @@ class TapReporter {
179195
if (fileStats.declaredTests === 0) {
180196
this.writeCrash(evt, `No tests found in ${path.relative('.', evt.testFile)}`);
181197
} else if (!this.failFastEnabled && fileStats.remainingTests > 0) {
182-
this.writeCrash(evt, `${fileStats.remainingTests} ${plur('test', fileStats.remainingTests)} remaining in ${path.relative('.', evt.testFile)}`);
198+
this.writeComment(evt, {title: `${fileStats.remainingTests} ${plur('test', fileStats.remainingTests)} remaining in ${path.relative('.', evt.testFile)}`});
183199
}
184200
}
185201

test/reporters/tap.failfast2.log

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ not ok 1 - a › fails
1010
...
1111
---tty-stream-chunk-separator
1212
# 1 test remaining in test/fixture/report/failfast2/a.js
13-
not ok 2 - 1 test remaining in test/fixture/report/failfast2/a.js
1413
---tty-stream-chunk-separator
1514

16-
1..1
17-
# tests 1
15+
1..2
16+
# tests 2
1817
# pass 0
1918
# fail 2
2019

test/reporters/tap.regular.log

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -240,25 +240,24 @@ not ok 26 - test › implementation throws non-error
240240
ok 27 - slow › slow
241241
---tty-stream-chunk-separator
242242
# output-in-hook › before hook
243-
ok 28 - output-in-hook › before hook
244243
---tty-stream-chunk-separator
245244
# output-in-hook › before hook
246-
ok 29 - output-in-hook › before hook
247-
* before
245+
---tty-stream-chunk-separator
246+
# before
248247
---tty-stream-chunk-separator
249248
# output-in-hook › beforeEach hook for passing test
250-
ok 30 - output-in-hook › beforeEach hook for passing test
251-
* beforeEach
249+
---tty-stream-chunk-separator
250+
# beforeEach
252251
---tty-stream-chunk-separator
253252
# output-in-hook › beforeEach hook for failing test
254-
ok 31 - output-in-hook › beforeEach hook for failing test
255-
* beforeEach
253+
---tty-stream-chunk-separator
254+
# beforeEach
256255
---tty-stream-chunk-separator
257256
# output-in-hook › passing test
258-
ok 32 - output-in-hook › passing test
257+
ok 28 - output-in-hook › passing test
259258
---tty-stream-chunk-separator
260259
# output-in-hook › failing test
261-
not ok 33 - output-in-hook › failing test
260+
not ok 29 - output-in-hook › failing test
262261
---
263262
name: AssertionError
264263
message: Test failed via `t.fail()`
@@ -267,20 +266,20 @@ not ok 33 - output-in-hook › failing test
267266
...
268267
---tty-stream-chunk-separator
269268
# output-in-hook › afterEach hook for passing test
270-
ok 34 - output-in-hook › afterEach hook for passing test
271-
* afterEach
269+
---tty-stream-chunk-separator
270+
# afterEach
272271
---tty-stream-chunk-separator
273272
# output-in-hook › afterEach.always hook for failing test
274-
ok 35 - output-in-hook › afterEach.always hook for failing test
275-
* afterEachAlways
273+
---tty-stream-chunk-separator
274+
# afterEachAlways
276275
---tty-stream-chunk-separator
277276
# output-in-hook › afterEach.always hook for passing test
278-
ok 36 - output-in-hook › afterEach.always hook for passing test
279-
* afterEachAlways
277+
---tty-stream-chunk-separator
278+
# afterEachAlways
280279
---tty-stream-chunk-separator
281280
# output-in-hook › cleanup
282-
ok 37 - output-in-hook › cleanup
283-
* afterAlways
281+
---tty-stream-chunk-separator
282+
# afterAlways
284283
---tty-stream-chunk-separator
285284

286285
1..23

0 commit comments

Comments
 (0)