Skip to content

Commit d3432ce

Browse files
committed
fix t.log in hooks
1 parent c3bcbf2 commit d3432ce

File tree

6 files changed

+132
-12
lines changed

6 files changed

+132
-12
lines changed

lib/reporters/verbose.js

+6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ class VerboseReporter {
126126
this.filesWithMissingAvaImports.add(evt.testFile);
127127
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`));
128128
break;
129+
case 'hook-finished':
130+
if (evt.logs.length > 0) {
131+
this.lineWriter.writeLine(` ${this.prefixTitle(evt.testFile, evt.title)}`);
132+
this.writeLogs(evt);
133+
}
134+
break;
129135
case 'selected-test':
130136
if (evt.skip) {
131137
this.lineWriter.writeLine(colors.skip(`- ${this.prefixTitle(evt.testFile, evt.title)}`));

lib/runner.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,6 @@ class Runner extends Emittery {
274274
title: `${task.title}${titleSuffix || ''}`
275275
}));
276276
return this.runMultiple(hooks, this.serial).then(outcome => {
277-
if (outcome.allPassed) {
278-
return true;
279-
}
280-
281-
// Only emit results for failed hooks.
282277
for (const result of outcome.storedResults) {
283278
if (!result.passed) {
284279
this.emit('stateChange', {
@@ -288,10 +283,17 @@ class Runner extends Emittery {
288283
duration: result.duration,
289284
logs: result.logs
290285
});
286+
} else {
287+
this.emit('stateChange', {
288+
type: 'hook-finished',
289+
title: result.title,
290+
duration: result.duration,
291+
logs: result.logs
292+
});
291293
}
292294
}
293295

294-
return false;
296+
return outcome.allPassed;
295297
});
296298
}
297299

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import test from '../../../..';
2+
3+
test.before(() => {});
4+
5+
test.before(t => {
6+
t.log('before');
7+
});
8+
9+
test.after('cleanup', t => {
10+
t.log('after');
11+
});
12+
13+
test.after.always('cleanup', t => {
14+
t.log('afterAlways');
15+
});
16+
17+
test.beforeEach(t => {
18+
t.log('beforeEach');
19+
});
20+
21+
test.afterEach(t => {
22+
t.log('afterEach');
23+
});
24+
25+
test.afterEach.always(t => {
26+
t.log('afterEachAlways');
27+
});
28+
29+
test('passing test', t => {
30+
t.pass();
31+
});
32+
33+
test('failing test', t => {
34+
t.fail();
35+
});

test/reporters/mini.regular.log

+29-1
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,26 @@ stderr
110110
1 skipped
111111
1 todo---tty-stream-chunk-separator
112112
---tty-stream-chunk-separator
113+
* output-in-hook › passing test
114+
115+
6 passed
116+
1 known failure
117+
8 tests failed
118+
1 skipped
119+
1 todo---tty-stream-chunk-separator
120+
---tty-stream-chunk-separator
121+
* output-in-hook › failing test
122+
123+
6 passed
124+
1 known failure
125+
9 tests failed
126+
1 skipped
127+
1 todo---tty-stream-chunk-separator
128+
---tty-stream-chunk-separator
113129
[?25h
114130
✖ No tests found in test/fixture/report/regular/bad-test-chain.js
115131

116-
8 tests failed
132+
9 tests failed
117133
1 known failure
118134
1 test skipped
119135
1 test todo
@@ -254,6 +270,18 @@ stderr
254270

255271

256272

273+
output-in-hook › failing test
274+
275+
~/test/fixture/report/regular/output-in-hook.js:34
276+
277+
33: test("failing test", t => {
278+
 34: t.fail(); 
279+
35: });
280+
281+
Test failed via `t.fail()`
282+
283+
284+
257285
Uncaught exception in test/fixture/report/regular/bad-test-chain.js
258286

259287
~/test/fixture/report/regular/bad-test-chain.js:3

test/reporters/tap.regular.log

+16-4
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,23 @@ not ok 21 - test › implementation throws non-error
154154
# slow › slow
155155
ok 22 - slow › slow
156156
---tty-stream-chunk-separator
157+
# output-in-hook › passing test
158+
ok 23 - output-in-hook › passing test
159+
---tty-stream-chunk-separator
160+
# output-in-hook › failing test
161+
not ok 24 - output-in-hook › failing test
162+
---
163+
name: AssertionError
164+
message: Test failed via `t.fail()`
165+
assertion: fail
166+
at: 'fail (output-in-hook.js:34:4)'
167+
...
168+
---tty-stream-chunk-separator
157169

158-
1..16
159-
# tests 15
160-
# pass 6
170+
1..18
171+
# tests 17
172+
# pass 7
161173
# skip 1
162-
# fail 15
174+
# fail 16
163175

164176
---tty-stream-chunk-separator

test/reporters/verbose.regular.log

+38-1
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,33 @@ stderr
8282
---tty-stream-chunk-separator
8383
✔ slow › slow (000ms)
8484
---tty-stream-chunk-separator
85+
output-in-hook › before hook
86+
ℹ before
87+
---tty-stream-chunk-separator
88+
output-in-hook › beforeEach hook for passing test
89+
ℹ beforeEach
90+
---tty-stream-chunk-separator
91+
output-in-hook › beforeEach hook for failing test
92+
ℹ beforeEach
93+
---tty-stream-chunk-separator
94+
✔ output-in-hook › passing test
95+
---tty-stream-chunk-separator
96+
✖ output-in-hook › failing test Test failed via `t.fail()`
97+
---tty-stream-chunk-separator
98+
output-in-hook › afterEach hook for passing test
99+
ℹ afterEach
100+
---tty-stream-chunk-separator
101+
output-in-hook › afterEach.always hook for failing test
102+
ℹ afterEachAlways
103+
---tty-stream-chunk-separator
104+
output-in-hook › afterEach.always hook for passing test
105+
ℹ afterEachAlways
106+
---tty-stream-chunk-separator
107+
output-in-hook › cleanup
108+
ℹ afterAlways
109+
---tty-stream-chunk-separator
85110

86-
8 tests failed
111+
9 tests failed
87112
1 known failure
88113
1 test skipped
89114
1 test todo
@@ -222,4 +247,16 @@ stderr
222247

223248
null
224249

250+
251+
252+
output-in-hook › failing test
253+
254+
~/test/fixture/report/regular/output-in-hook.js:34
255+
256+
33: test("failing test", t => {
257+
 34: t.fail(); 
258+
35: });
259+
260+
Test failed via `t.fail()`
261+
225262
---tty-stream-chunk-separator

0 commit comments

Comments
 (0)