Skip to content

fix t.log in hooks #2012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/reporters/verbose.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,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 @@ -291,7 +293,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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