Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to type t.context by extending module interface #3362

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/** 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
Loading