@@ -58,15 +58,27 @@ test(async t => {
58
58
59
59
## Using [ macros] ( ../01-writing-tests.md#reusing-test-logic-through-macros )
60
60
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
+
61
73
In order to be able to assign the ` title ` property to a macro you need to type the function:
62
74
63
75
``` ts
64
76
import test , {Macro } from ' ava' ;
65
77
66
- const macro: Macro = (t , input : string , expected : number ) => {
78
+ const macro: Macro <[ string , number ]> = (t , input , expected ) => {
67
79
t .is (eval (input ), expected );
68
80
};
69
- macro .title = (providedTitle = ' ' , input : string , expected : number ) => ` ${providedTitle } ${input } = ${expected } ` .trim ();
81
+ macro .title = (providedTitle = ' ' , input , expected ) => ` ${providedTitle } ${input } = ${expected } ` .trim ();
70
82
71
83
test (macro , ' 2 + 2' , 4 );
72
84
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
78
90
``` ts
79
91
import test , {CbMacro } from ' ava' ;
80
92
81
- const macro: CbMacro = t => {
93
+ const macro: CbMacro <[]> = t => {
82
94
t .pass ();
83
95
setTimeout (t .end , 100 );
84
96
};
@@ -123,7 +135,7 @@ interface Context {
123
135
124
136
const test = anyTest as TestInterface <Context >;
125
137
126
- const macro: Macro <Context > = (t , expected : string ) => {
138
+ const macro: Macro <[ string ], Context > = (t , expected : string ) => {
127
139
t .is (t .context .foo , expected );
128
140
};
129
141
0 commit comments