Skip to content

Commit 900dc6d

Browse files
authoredSep 9, 2020
Make t.passed usable in tests and teardown functions
1 parent f42e0e0 commit 900dc6d

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed
 

‎docs/02-execution-context.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Contains shared state from hooks.
2424

2525
## `t.passed`
2626

27-
Whether a test has passed. This value is only accurate in the `test.afterEach()` and `test.afterEach.always()` hooks.
27+
When used in `test.afterEach()` or `test.afterEach.always()` hooks this tells you whether the test has passed. When used in a test itself (including teardown functions) this remains `true` until an assertion fails, the test has ended with an error, or a teardown function caused an error. This value has no meaning in other hooks.
2828

2929
## `t.end()`
3030

@@ -36,7 +36,7 @@ Log values contextually alongside the test result instead of immediately printin
3636

3737
## `t.plan(count)`
3838

39-
Plan how many assertion there are in the test. The test will fail if the actual assertion count doesn't match the number of planned assertions. See [assertion planning](./03-assertions.md#assertion-planning).
39+
Plan how many assertions there are in the test. The test will fail if the actual assertion count doesn't match the number of planned assertions. See [assertion planning](./03-assertions.md#assertion-planning).
4040

4141
## `t.teardown(fn)`
4242

‎lib/test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ class ExecutionContext extends assert.Assertions {
187187
}
188188

189189
get passed() {
190-
return testMap.get(this).testPassed;
190+
const test = testMap.get(this);
191+
return test.isHook ? test.testPassed : !test.assertError;
191192
}
192193

193194
_throwsArgStart(assertion, file, line) {

‎test-tap/test.js

+30
Original file line numberDiff line numberDiff line change
@@ -956,3 +956,33 @@ test('.teardown() is bound', t => {
956956
t.ok(teardownCallback.calledOnce);
957957
});
958958
});
959+
960+
test('t.passed value is true when teardown callback is executed for passing test', t => {
961+
new Test({
962+
fn(a) {
963+
a.teardown(() => {
964+
t.is(a.passed, true);
965+
t.end();
966+
});
967+
a.pass();
968+
},
969+
metadata: {type: 'test'},
970+
onResult() {},
971+
title: 'foo'
972+
}).run();
973+
});
974+
975+
test('t.passed value is false when teardown callback is executed for failing test', t => {
976+
new Test({
977+
fn(a) {
978+
a.teardown(() => {
979+
t.is(a.passed, false);
980+
t.end();
981+
});
982+
a.fail();
983+
},
984+
metadata: {type: 'test'},
985+
onResult() {},
986+
title: 'foo'
987+
}).run();
988+
});

0 commit comments

Comments
 (0)
Please sign in to comment.