Skip to content

Commit

Permalink
Make t.passed usable in tests and teardown functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tymfear committed Sep 9, 2020
1 parent f42e0e0 commit 900dc6d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/02-execution-context.md
Expand Up @@ -24,7 +24,7 @@ Contains shared state from hooks.

## `t.passed`

Whether a test has passed. This value is only accurate in the `test.afterEach()` and `test.afterEach.always()` hooks.
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.

## `t.end()`

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

## `t.plan(count)`

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).
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).

## `t.teardown(fn)`

Expand Down
3 changes: 2 additions & 1 deletion lib/test.js
Expand Up @@ -187,7 +187,8 @@ class ExecutionContext extends assert.Assertions {
}

get passed() {
return testMap.get(this).testPassed;
const test = testMap.get(this);
return test.isHook ? test.testPassed : !test.assertError;
}

_throwsArgStart(assertion, file, line) {
Expand Down
30 changes: 30 additions & 0 deletions test-tap/test.js
Expand Up @@ -956,3 +956,33 @@ test('.teardown() is bound', t => {
t.ok(teardownCallback.calledOnce);
});
});

test('t.passed value is true when teardown callback is executed for passing test', t => {
new Test({
fn(a) {
a.teardown(() => {
t.is(a.passed, true);
t.end();
});
a.pass();
},
metadata: {type: 'test'},
onResult() {},
title: 'foo'
}).run();
});

test('t.passed value is false when teardown callback is executed for failing test', t => {
new Test({
fn(a) {
a.teardown(() => {
t.is(a.passed, false);
t.end();
});
a.fail();
},
metadata: {type: 'test'},
onResult() {},
title: 'foo'
}).run();
});

0 comments on commit 900dc6d

Please sign in to comment.