Skip to content

Commit

Permalink
Print hooks and remaining tests as TAP comments
Browse files Browse the repository at this point in the history
Fixes #2142.
  • Loading branch information
yovasx2 authored and novemberborn committed Jul 30, 2019
1 parent f01d05f commit 8628976
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
24 changes: 20 additions & 4 deletions lib/reporters/tap.js
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const plur = require('plur');
const stripAnsi = require('strip-ansi');
const supertap = require('supertap');
const indentString = require('indent-string');

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

Expand Down Expand Up @@ -74,7 +75,7 @@ class TapReporter {
if (this.stats) {
this.reportStream.write(supertap.finish({
crashed: this.crashCount,
failed: this.stats.failedHooks + this.stats.failedTests,
failed: this.stats.failedTests + this.stats.remainingTests,
passed: this.stats.passedTests + this.stats.passedKnownFailingTests,
skipped: this.stats.skippedTests,
todo: this.stats.todoTests
Expand Down Expand Up @@ -118,6 +119,21 @@ class TapReporter {
}) + os.EOL);
}

writeComment(evt, {error = false, title = this.prefixTitle(evt.testFile, evt.title)}) {
let formattedTitle = title;
if (error) {
formattedTitle = `Failed hook: ${formattedTitle}`;
}

this.reportStream.write(`# ${stripAnsi(formattedTitle)}${os.EOL}`);
if (evt.logs) {
for (const log of evt.logs) {
const logLines = indentString(log, 4).replace(/^ {4}/, ' # ');
this.reportStream.write(`${logLines}${os.EOL}`);
}
}
}

consumeStateChange(evt) { // eslint-disable-line complexity
const fileStats = this.stats && evt.testFile ? this.stats.byFile.get(evt.testFile) : null;

Expand All @@ -126,10 +142,10 @@ class TapReporter {
// Ignore
break;
case 'hook-failed':
this.writeTest(evt, {passed: false, todo: false, skip: false});
this.writeComment(evt, {error: true});
break;
case 'hook-finished':
this.writeTest(evt, {passed: true, todo: false, skip: false});
this.writeComment(evt, {});
break;
case 'internal-error':
this.writeCrash(evt);
Expand Down Expand Up @@ -179,7 +195,7 @@ class TapReporter {
if (fileStats.declaredTests === 0) {
this.writeCrash(evt, `No tests found in ${path.relative('.', evt.testFile)}`);
} else if (!this.failFastEnabled && fileStats.remainingTests > 0) {
this.writeCrash(evt, `${fileStats.remainingTests} ${plur('test', fileStats.remainingTests)} remaining in ${path.relative('.', evt.testFile)}`);
this.writeComment(evt, {title: `${fileStats.remainingTests} ${plur('test', fileStats.remainingTests)} remaining in ${path.relative('.', evt.testFile)}`});
}
}

Expand Down
5 changes: 2 additions & 3 deletions test/reporters/tap.failfast2.log
Expand Up @@ -10,11 +10,10 @@ not ok 1 - a › fails
...
---tty-stream-chunk-separator
# 1 test remaining in test/fixture/report/failfast2/a.js
not ok 2 - 1 test remaining in test/fixture/report/failfast2/a.js
---tty-stream-chunk-separator

1..1
# tests 1
1..2
# tests 2
# pass 0
# fail 2

Expand Down
33 changes: 16 additions & 17 deletions test/reporters/tap.regular.log
Expand Up @@ -240,25 +240,24 @@ not ok 26 - test › implementation throws non-error
ok 27 - slow › slow
---tty-stream-chunk-separator
# output-in-hook › before hook
ok 28 - output-in-hook › before hook
---tty-stream-chunk-separator
# output-in-hook › before hook
ok 29 - output-in-hook › before hook
* before
---tty-stream-chunk-separator
# before
---tty-stream-chunk-separator
# output-in-hook › beforeEach hook for passing test
ok 30 - output-in-hook › beforeEach hook for passing test
* beforeEach
---tty-stream-chunk-separator
# beforeEach
---tty-stream-chunk-separator
# output-in-hook › beforeEach hook for failing test
ok 31 - output-in-hook › beforeEach hook for failing test
* beforeEach
---tty-stream-chunk-separator
# beforeEach
---tty-stream-chunk-separator
# output-in-hook › passing test
ok 32 - output-in-hook › passing test
ok 28 - output-in-hook › passing test
---tty-stream-chunk-separator
# output-in-hook › failing test
not ok 33 - output-in-hook › failing test
not ok 29 - output-in-hook › failing test
---
name: AssertionError
message: Test failed via `t.fail()`
Expand All @@ -267,20 +266,20 @@ not ok 33 - output-in-hook › failing test
...
---tty-stream-chunk-separator
# output-in-hook › afterEach hook for passing test
ok 34 - output-in-hook › afterEach hook for passing test
* afterEach
---tty-stream-chunk-separator
# afterEach
---tty-stream-chunk-separator
# output-in-hook › afterEach.always hook for failing test
ok 35 - output-in-hook › afterEach.always hook for failing test
* afterEachAlways
---tty-stream-chunk-separator
# afterEachAlways
---tty-stream-chunk-separator
# output-in-hook › afterEach.always hook for passing test
ok 36 - output-in-hook › afterEach.always hook for passing test
* afterEachAlways
---tty-stream-chunk-separator
# afterEachAlways
---tty-stream-chunk-separator
# output-in-hook › cleanup
ok 37 - output-in-hook › cleanup
* afterAlways
---tty-stream-chunk-separator
# afterAlways
---tty-stream-chunk-separator

1..23
Expand Down

0 comments on commit 8628976

Please sign in to comment.