Skip to content

Commit

Permalink
Type additional arguments used by macros
Browse files Browse the repository at this point in the history
Co-authored-by: Nikita Volodin [Никита Володин] <volodin.n@gmail.com>
  • Loading branch information
novemberborn and qlonik committed Nov 11, 2018
1 parent a82fee5 commit 6f54db8
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 72 deletions.
20 changes: 16 additions & 4 deletions docs/recipes/typescript.md
Expand Up @@ -58,15 +58,27 @@ test(async t => {

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

Macros can receive additional arguments. AVA can infer these to ensure you're using the macro correctly:

```ts
import test, {ExecutionContext} from 'ava';

const hasLength = (t: ExecutionContext, input: string, expected: number) => {
t.is(input.length, expected);
};

test('bar has length 3', hasLength, 'bar', 3);
```

In order to be able to assign the `title` property to a macro you need to type the function:

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

const macro: Macro = (t, input: string, expected: number) => {
const macro: Macro<[string, number]> = (t, input, expected) => {
t.is(eval(input), expected);
};
macro.title = (providedTitle = '', input: string, expected: number) => `${providedTitle} ${input} = ${expected}`.trim();
macro.title = (providedTitle = '', input, expected) => `${providedTitle} ${input} = ${expected}`.trim();

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

const macro: CbMacro = t => {
const macro: CbMacro<[]> = t => {
t.pass();
setTimeout(t.end, 100);
};
Expand Down Expand Up @@ -123,7 +135,7 @@ interface Context {

const test = anyTest as TestInterface<Context>;

const macro: Macro<Context> = (t, expected: string) => {
const macro: Macro<[string], Context> = (t, expected: string) => {
t.is(t.context.foo, expected);
};

Expand Down

0 comments on commit 6f54db8

Please sign in to comment.