Skip to content

Commit 932a770

Browse files
authored
Document context support in before & after hooks
Fixes #1579.
1 parent 29ae307 commit 932a770

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

readme.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ Remember that AVA runs each test file in its own process. You may not have to cl
602602

603603
#### Test context
604604

605-
The `.beforeEach()` & `.afterEach()` hooks can share context with the test:
605+
Hooks can share context with the test:
606606

607607
```js
608608
test.beforeEach(t => {
@@ -614,10 +614,14 @@ test('context data is foo', t => {
614614
});
615615
```
616616

617-
The context is not shared between tests, allowing you to set up data in a way where it will not risk leaking to other, subsequent tests. By default `t.context` is an object but you can reassign it:
617+
Context created in `.before()` hooks is [cloned](https://www.npmjs.com/package/lodash.clone) before it is passed to `.beforeEach()` hooks and / or tests. The `.after()` and `.after.always()` hooks receive the original context value.
618+
619+
For `.beforeEach()`, `.afterEach()` and `.afterEach.always()` hooks the context is *not* shared between different tests, allowing you to set up data such that it will not leak to other tests.
620+
621+
By default `t.context` is an object but you can reassign it:
618622

619623
```js
620-
test.beforeEach(t => {
624+
test.before(t => {
621625
t.context = 'unicorn';
622626
});
623627

@@ -626,8 +630,6 @@ test('context is unicorn', t => {
626630
});
627631
```
628632

629-
Context sharing is *not* available to `.before()` and `.after()` hooks.
630-
631633
### Test macros
632634

633635
Additional arguments passed to the test declaration will be passed to the test implementation. This is useful for creating reusable test macros.
@@ -827,7 +829,7 @@ Should contain the actual test.
827829

828830
Type: `object`
829831

830-
The execution object of a particular test. Each test implementation receives a different object. Contains the [assertions](#assertions) as well as `.plan(count)` and `.end()` methods. `t.context` can contain shared state from `.beforeEach()` hooks. `t.title` returns the test's title.
832+
The execution object of a particular test. Each test implementation receives a different object. Contains the [assertions](#assertions) as well as `.plan(count)` and `.end()` methods. `t.context` can contain shared state from hooks. `t.title` returns the test's title.
831833

832834
###### `t.plan(count)`
833835

0 commit comments

Comments
 (0)