Skip to content

Commit 6f54db8

Browse files
novemberbornqlonik
andauthored
Type additional arguments used by macros
Co-authored-by: Nikita Volodin [Никита Володин] <volodin.n@gmail.com>
1 parent a82fee5 commit 6f54db8

File tree

4 files changed

+207
-72
lines changed

4 files changed

+207
-72
lines changed

docs/recipes/typescript.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,27 @@ test(async t => {
5858

5959
## Using [macros](../01-writing-tests.md#reusing-test-logic-through-macros)
6060

61+
Macros can receive additional arguments. AVA can infer these to ensure you're using the macro correctly:
62+
63+
```ts
64+
import test, {ExecutionContext} from 'ava';
65+
66+
const hasLength = (t: ExecutionContext, input: string, expected: number) => {
67+
t.is(input.length, expected);
68+
};
69+
70+
test('bar has length 3', hasLength, 'bar', 3);
71+
```
72+
6173
In order to be able to assign the `title` property to a macro you need to type the function:
6274

6375
```ts
6476
import test, {Macro} from 'ava';
6577

66-
const macro: Macro = (t, input: string, expected: number) => {
78+
const macro: Macro<[string, number]> = (t, input, expected) => {
6779
t.is(eval(input), expected);
6880
};
69-
macro.title = (providedTitle = '', input: string, expected: number) => `${providedTitle} ${input} = ${expected}`.trim();
81+
macro.title = (providedTitle = '', input, expected) => `${providedTitle} ${input} = ${expected}`.trim();
7082

7183
test(macro, '2 + 2', 4);
7284
test(macro, '2 * 3', 6);
@@ -78,7 +90,7 @@ You'll need a different type if you're expecting your macro to be used with a ca
7890
```ts
7991
import test, {CbMacro} from 'ava';
8092

81-
const macro: CbMacro = t => {
93+
const macro: CbMacro<[]> = t => {
8294
t.pass();
8395
setTimeout(t.end, 100);
8496
};
@@ -123,7 +135,7 @@ interface Context {
123135

124136
const test = anyTest as TestInterface<Context>;
125137

126-
const macro: Macro<Context> = (t, expected: string) => {
138+
const macro: Macro<[string], Context> = (t, expected: string) => {
127139
t.is(t.context.foo, expected);
128140
};
129141

0 commit comments

Comments
 (0)