You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/01-writing-tests.md
+2
Original file line number
Diff line number
Diff line change
@@ -167,6 +167,8 @@ AVA lets you register hooks that are run before and after your tests. This allow
167
167
168
168
If a test is skipped with the `.skip` modifier, the respective `.beforeEach()`, `.afterEach()` and `.afterEach.always()` hooks are not run. Likewise, if all tests in a test file are skipped `.before()`, `.after()` and `.after.always()` hooks for the file are not run.
169
169
170
+
*You may not need to use `.afterEach.always()` hooks to clean up after a test.* You can use [`t.teardown()`](./02-execution-context.md#tteardownfn) to undo side-effects *within* a particular test.
171
+
170
172
Like `test()` these methods take an optional title and an implementation function. The title is shown if your hook fails to execute. The implementation is called with an [execution object](./02-execution-context.md). You can use assertions in your hooks. You can also pass a [macro function](#reusing-test-logic-through-macros) and additional arguments.
171
173
172
174
`.before()` hooks execute before `.beforeEach()` hooks. `.afterEach()` hooks execute before `.after()` hooks. Within their category the hooks execute in the order they were defined. By default hooks execute concurrently, but you can use `test.serial` to ensure only that single hook is run at a time. Unlike with tests, serial hooks are *not* run before other hooks:
Copy file name to clipboardexpand all lines: docs/02-execution-context.md
+12-4
Original file line number
Diff line number
Diff line change
@@ -26,10 +26,6 @@ Contains shared state from hooks.
26
26
27
27
Whether a test has passed. This value is only accurate in the `test.afterEach()` and `test.afterEach.always()` hooks.
28
28
29
-
## `t.plan(count)`
30
-
31
-
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).
32
-
33
29
## `t.end()`
34
30
35
31
End the test. Only works with `test.cb()`.
@@ -38,6 +34,18 @@ End the test. Only works with `test.cb()`.
38
34
39
35
Log values contextually alongside the test result instead of immediately printing them to `stdout`. Behaves somewhat like `console.log`, but without support for placeholder tokens.
40
36
37
+
## `t.plan(count)`
38
+
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).
40
+
41
+
## `t.teardown(fn)`
42
+
43
+
Registers the `fn` function to be run after the test has finished. You can register multiple functions and they'll run in order. You can use asynchronous functions: only one will run at a time.
44
+
45
+
You cannot perform assertions using the `t` object or register additional functions from inside `fn`.
46
+
47
+
You cannot use `t.teardown()` in hooks either.
48
+
41
49
## `t.timeout(ms)`
42
50
43
51
Set a timeout for the test, in milliseconds. The test will fail if this timeout is exceeded. The timeout is reset each time an assertion is made.
Copy file name to clipboardexpand all lines: docs/recipes/test-setup.md
+2
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,8 @@ test('second scenario', t => {
64
64
});
65
65
```
66
66
67
+
You can use [`t.teardown()`](../02-execution-context.md#tteardownfn) to register a teardown function which will run after the test has finished (regardless of whether it's passed or failed).
0 commit comments