Skip to content

Commit

Permalink
feat: create AvaContext to augument by library user
Browse files Browse the repository at this point in the history
  • Loading branch information
acro5piano committed Dec 24, 2024
1 parent 024de32 commit b88bc3d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/recipes/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ test('an actual test', t => {
});
```

Alternatively, you can extend the type globally:

```ts
declare module 'ava' {
interface AvaContext {
foo: string;
}
}

test.beforeEach(t => {
t.context = {foo: 'bar'};
});
```

Note that, despite the type cast above, when executing `t.context` is an empty object unless it's assigned.

## Typing `throws` assertions
Expand Down
4 changes: 3 additions & 1 deletion types/test-fn.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export type Macro<Args extends unknown[], Context = unknown> = {
/** A test or hook implementation. */
export type Implementation<Args extends unknown[], Context = unknown> = ImplementationFn<Args, Context> | Macro<Args, Context>;

export type TestFn<Context = unknown> = {
interface AvaContext {}

Check failure on line 75 in types/test-fn.d.cts

View workflow job for this annotation

GitHub Actions / Lint source files

Use a `type` instead of an `interface`.

Check failure on line 75 in types/test-fn.d.cts

View workflow job for this annotation

GitHub Actions / Lint source files

An empty interface is equivalent to `{}`.

export type TestFn<Context = AvaContext> = {
/** Declare a concurrent test. Additional arguments are passed to the implementation or macro. */
<Args extends unknown[]>(title: string, implementation: Implementation<Args, Context>, ...args: Args): void;

Expand Down

0 comments on commit b88bc3d

Please sign in to comment.