Skip to content

Commit

Permalink
Fix t.log() in hooks
Browse files Browse the repository at this point in the history
Fixes #1536
  • Loading branch information
dflupu authored and novemberborn committed Jan 13, 2019
1 parent bccd297 commit d187712
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 13 deletions.
6 changes: 6 additions & 0 deletions lib/reporters/verbose.js
Expand Up @@ -126,6 +126,12 @@ class VerboseReporter {
this.filesWithMissingAvaImports.add(evt.testFile);
this.lineWriter.writeLine(colors.error(`${figures.cross} No tests found in ${path.relative('.', evt.testFile)}, make sure to import "ava" at the top of your test file`));
break;
case 'hook-finished':
if (evt.logs.length > 0) {
this.lineWriter.writeLine(` ${this.prefixTitle(evt.testFile, evt.title)}`);
this.writeLogs(evt);
}
break;
case 'selected-test':
if (evt.skip) {
this.lineWriter.writeLine(colors.skip(`- ${this.prefixTitle(evt.testFile, evt.title)}`));
Expand Down
16 changes: 9 additions & 7 deletions lib/runner.js
Expand Up @@ -277,13 +277,15 @@ class Runner extends Emittery {
title: `${task.title}${titleSuffix || ''}`
}));
return this.runMultiple(hooks, this.serial).then(outcome => {
if (outcome.allPassed) {
return true;
}

// Only emit results for failed hooks.
for (const result of outcome.storedResults) {
if (!result.passed) {
if (result.passed) {
this.emit('stateChange', {
type: 'hook-finished',
title: result.title,
duration: result.duration,
logs: result.logs
});
} else {
this.emit('stateChange', {
type: 'hook-failed',
title: result.title,
Expand All @@ -294,7 +296,7 @@ class Runner extends Emittery {
}
}

return false;
return outcome.allPassed;
});
}

Expand Down
35 changes: 35 additions & 0 deletions test/fixture/report/regular/output-in-hook.js
@@ -0,0 +1,35 @@
import test from '../../../..';

test.before(() => {});

test.before(t => {
t.log('before');
});

test.after('cleanup', t => {
t.log('after');
});

test.after.always('cleanup', t => {
t.log('afterAlways');
});

test.beforeEach(t => {
t.log('beforeEach');
});

test.afterEach(t => {
t.log('afterEach');
});

test.afterEach.always(t => {
t.log('afterEachAlways');
});

test('passing test', t => {
t.pass();
});

test('failing test', t => {
t.fail();
});
30 changes: 29 additions & 1 deletion test/reporters/mini.regular.log
Expand Up @@ -110,10 +110,26 @@ stderr
1 skipped
1 todo---tty-stream-chunk-separator
---tty-stream-chunk-separator
* output-in-hook › passing test

6 passed
1 known failure
8 tests failed
1 skipped
1 todo---tty-stream-chunk-separator
---tty-stream-chunk-separator
* output-in-hook › failing test

6 passed
1 known failure
9 tests failed
1 skipped
1 todo---tty-stream-chunk-separator
---tty-stream-chunk-separator
[?25h
✖ No tests found in test/fixture/report/regular/bad-test-chain.js

8 tests failed
9 tests failed
1 known failure
1 test skipped
1 test todo
Expand Down Expand Up @@ -254,6 +270,18 @@ stderr



output-in-hook › failing test

~/test/fixture/report/regular/output-in-hook.js:34

33: test('failing test', t => {
 34: t.fail(); 
35: });

Test failed via `t.fail()`



Uncaught exception in test/fixture/report/regular/bad-test-chain.js

~/test/fixture/report/regular/bad-test-chain.js:3
Expand Down
20 changes: 16 additions & 4 deletions test/reporters/tap.regular.log
Expand Up @@ -154,11 +154,23 @@ not ok 21 - test › implementation throws non-error
# slow › slow
ok 22 - slow › slow
---tty-stream-chunk-separator
# output-in-hook › passing test
ok 23 - output-in-hook › passing test
---tty-stream-chunk-separator
# output-in-hook › failing test
not ok 24 - output-in-hook › failing test
---
name: AssertionError
message: Test failed via `t.fail()`
assertion: fail
at: 'fail (output-in-hook.js:34:4)'
...
---tty-stream-chunk-separator

1..16
# tests 15
# pass 6
1..18
# tests 17
# pass 7
# skip 1
# fail 15
# fail 16

---tty-stream-chunk-separator
39 changes: 38 additions & 1 deletion test/reporters/verbose.regular.log
Expand Up @@ -82,8 +82,33 @@ stderr
---tty-stream-chunk-separator
✔ slow › slow (000ms)
---tty-stream-chunk-separator
output-in-hook › before hook
ℹ before
---tty-stream-chunk-separator
output-in-hook › beforeEach hook for passing test
ℹ beforeEach
---tty-stream-chunk-separator
output-in-hook › beforeEach hook for failing test
ℹ beforeEach
---tty-stream-chunk-separator
✔ output-in-hook › passing test
---tty-stream-chunk-separator
✖ output-in-hook › failing test Test failed via `t.fail()`
---tty-stream-chunk-separator
output-in-hook › afterEach hook for passing test
ℹ afterEach
---tty-stream-chunk-separator
output-in-hook › afterEach.always hook for failing test
ℹ afterEachAlways
---tty-stream-chunk-separator
output-in-hook › afterEach.always hook for passing test
ℹ afterEachAlways
---tty-stream-chunk-separator
output-in-hook › cleanup
ℹ afterAlways
---tty-stream-chunk-separator

8 tests failed
9 tests failed
1 known failure
1 test skipped
1 test todo
Expand Down Expand Up @@ -222,4 +247,16 @@ stderr

null



output-in-hook › failing test

~/test/fixture/report/regular/output-in-hook.js:34

33: test('failing test', t => {
 34: t.fail(); 
35: });

Test failed via `t.fail()`

---tty-stream-chunk-separator

0 comments on commit d187712

Please sign in to comment.