Skip to content

Commit a898154

Browse files
Add StackBlitz examples for TypeScript
1 parent 58150a0 commit a898154

File tree

12 files changed

+104
-1
lines changed

12 files changed

+104
-1
lines changed

.xo-config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"media/**",
44
"test/config/fixtures/config-errors/test.js",
55
"test-tap/fixture/snapshots/test-sourcemaps/build/**",
6-
"test-tap/fixture/report/edgecases/ast-syntax-error.cjs"
6+
"test-tap/fixture/report/edgecases/ast-syntax-error.cjs",
7+
"examples/typescript-*/**/*.ts"
78
],
89
"rules": {
910
"import/no-anonymous-default-export": "off",

docs/recipes/typescript.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ It's worth noting that with this configuration, tests will fail if there are Typ
9595

9696
## Writing tests
9797

98+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-basic?file=source%2Ftest.ts&terminal=test&view=editor)
99+
98100
Create a `test.ts` file.
99101

100102
```ts
@@ -186,6 +188,8 @@ test('providedTitle', macro, '3 * 3', 9);
186188

187189
## Typing [`t.context`](../01-writing-tests.md#test-context)
188190

191+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-context?file=source%2Ftest.ts&terminal=test&view=editor)
192+
189193
By default, the type of `t.context` will be the empty object (`{}`). AVA exposes an interface `TestInterface<Context>` (in AVA 4 this is `TestFn<Context>`) which you can use to apply your own type to `t.context`. This can help you catch errors at compile-time:
190194

191195
```ts
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "ava-typescript-basic",
3+
"description": "Basic example for AVA with TypeScript",
4+
"scripts": {
5+
"test": "ava"
6+
},
7+
"devDependencies": {
8+
"@ava/typescript": "^2.0.0",
9+
"@sindresorhus/tsconfig": "^1.0.2",
10+
"ava": "^3.15.0",
11+
"typescript": "^4.3.4"
12+
},
13+
"ava": {
14+
"typescript": {
15+
"compile": "tsc",
16+
"rewritePaths": {
17+
"source/": "build/"
18+
}
19+
}
20+
}
21+
}

examples/typescript-basic/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TypeScript basic example
2+
3+
> Basic example for [AVA with TypeScript](https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md)
4+
5+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-basic?file=source%2Ftest.ts&terminal=test&view=editor)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const sum = (a: number, b: number) => a + b;
2+
3+
export const subtract = (a: number, b: number) => a - b;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import test from 'ava';
2+
3+
import {sum, subtract} from '.';
4+
5+
test('sum', t => {
6+
t.is(sum(0, 0), 0);
7+
t.is(sum(2, 2), 4);
8+
});
9+
10+
test('subtract', t => {
11+
t.is(subtract(0, 0), 0);
12+
t.is(subtract(4, 2), 2);
13+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "@sindresorhus/tsconfig",
3+
"compilerOptions": {
4+
"module": "commonjs",
5+
"outDir": "build",
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "ava-typescript-context",
3+
"description": "TypeScript example for typing t.context",
4+
"scripts": {
5+
"test": "ava"
6+
},
7+
"devDependencies": {
8+
"@ava/typescript": "^2.0.0",
9+
"@sindresorhus/tsconfig": "^1.0.2",
10+
"ava": "^3.15.0",
11+
"typescript": "^4.3.4"
12+
},
13+
"ava": {
14+
"typescript": {
15+
"compile": "tsc",
16+
"rewritePaths": {
17+
"source/": "build/"
18+
}
19+
}
20+
}
21+
}

examples/typescript-context/readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TypeScript context example
2+
3+
> TypeScript example for [typing `t.context`](https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md#typing-tcontext)
4+
5+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-context?file=source%2Ftest.ts&terminal=test&view=editor)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const concat = (input: string[]) => input.join(' ');

0 commit comments

Comments
 (0)