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/recipes/typescript.md
+30-1Lines changed: 30 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ AVA comes bundled with a TypeScript definition file. This allows developers to l
6
6
7
7
## Setup
8
8
9
-
First install [TypeScript](https://github.com/Microsoft/TypeScript).
9
+
First install [TypeScript](https://github.com/Microsoft/TypeScript) (if you already have it installed, make sure you use version 2.1 or greater).
10
10
11
11
```
12
12
$ npm install --save-dev typescript
@@ -50,6 +50,35 @@ test(async (t) => {
50
50
});
51
51
```
52
52
53
+
## Working with [`context`](https://github.com/avajs/ava#test-context)
54
+
55
+
By default, the type of `t.context` will be [`any`](https://www.typescriptlang.org/docs/handbook/basic-types.html#any). AVA exposes an interface `RegisterContextual<T>` which you can use to apply your own type to `t.context`. This can help you catch errors at compile-time:
56
+
57
+
```ts
58
+
import*asavafrom'ava';
59
+
60
+
function contextualize<T>(getContext: () =>T):ava.RegisterContextual<T> {
61
+
ava.test.beforeEach(t=> {
62
+
Object.assign(t.context, getContext());
63
+
});
64
+
65
+
returnava.test;
66
+
}
67
+
68
+
const test =contextualize(() => ({ foo: 'bar' }));
69
+
70
+
test.beforeEach(t=> {
71
+
t.context.foo=123; // error: Type '123' is not assignable to type 'string'
72
+
});
73
+
74
+
test.after.always.failing.cb.serial('very long chains are properly typed', t=> {
75
+
t.context.fooo='a value'; // error: Property 'fooo' does not exist on type '{ foo: string }'
76
+
});
77
+
78
+
test('an actual test', t=> {
79
+
t.deepEqual(t.context.foo.map(c=>c), ['b', 'a', 'r']); // error: Property 'map' does not exist on type 'string'
Copy file name to clipboardExpand all lines: readme.md
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -578,6 +578,8 @@ Keep in mind that the `beforeEach` and `afterEach` hooks run just before and aft
578
578
579
579
Remember that AVA runs each test file in its own process. You may not have to clean up global state in a `after`-hook since that's only called right before the process exits.
580
580
581
+
#### Test context
582
+
581
583
The `beforeEach` & `afterEach` hooks can share context with the test:
0 commit comments