Add StackBlitz examples for TypeScript
This commit is contained in:
parent
58150a0c9e
commit
a898154010
12 changed files with 104 additions and 1 deletions
|
|
@ -3,7 +3,8 @@
|
|||
"media/**",
|
||||
"test/config/fixtures/config-errors/test.js",
|
||||
"test-tap/fixture/snapshots/test-sourcemaps/build/**",
|
||||
"test-tap/fixture/report/edgecases/ast-syntax-error.cjs"
|
||||
"test-tap/fixture/report/edgecases/ast-syntax-error.cjs",
|
||||
"examples/typescript-*/**/*.ts"
|
||||
],
|
||||
"rules": {
|
||||
"import/no-anonymous-default-export": "off",
|
||||
|
|
|
|||
|
|
@ -95,6 +95,8 @@ It's worth noting that with this configuration, tests will fail if there are Typ
|
|||
|
||||
## Writing tests
|
||||
|
||||
[](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-basic?file=source%2Ftest.ts&terminal=test&view=editor)
|
||||
|
||||
Create a `test.ts` file.
|
||||
|
||||
```ts
|
||||
|
|
@ -186,6 +188,8 @@ test('providedTitle', macro, '3 * 3', 9);
|
|||
|
||||
## Typing [`t.context`](../01-writing-tests.md#test-context)
|
||||
|
||||
[](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-context?file=source%2Ftest.ts&terminal=test&view=editor)
|
||||
|
||||
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:
|
||||
|
||||
```ts
|
||||
|
|
|
|||
21
examples/typescript-basic/package.json
Normal file
21
examples/typescript-basic/package.json
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "ava-typescript-basic",
|
||||
"description": "Basic example for AVA with TypeScript",
|
||||
"scripts": {
|
||||
"test": "ava"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ava/typescript": "^2.0.0",
|
||||
"@sindresorhus/tsconfig": "^1.0.2",
|
||||
"ava": "^3.15.0",
|
||||
"typescript": "^4.3.4"
|
||||
},
|
||||
"ava": {
|
||||
"typescript": {
|
||||
"compile": "tsc",
|
||||
"rewritePaths": {
|
||||
"source/": "build/"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
5
examples/typescript-basic/readme.md
Normal file
5
examples/typescript-basic/readme.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# TypeScript basic example
|
||||
|
||||
> Basic example for [AVA with TypeScript](https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md)
|
||||
|
||||
[](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-basic?file=source%2Ftest.ts&terminal=test&view=editor)
|
||||
3
examples/typescript-basic/source/index.ts
Normal file
3
examples/typescript-basic/source/index.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export const sum = (a: number, b: number) => a + b;
|
||||
|
||||
export const subtract = (a: number, b: number) => a - b;
|
||||
13
examples/typescript-basic/source/test.ts
Normal file
13
examples/typescript-basic/source/test.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import test from 'ava';
|
||||
|
||||
import {sum, subtract} from '.';
|
||||
|
||||
test('sum', t => {
|
||||
t.is(sum(0, 0), 0);
|
||||
t.is(sum(2, 2), 4);
|
||||
});
|
||||
|
||||
test('subtract', t => {
|
||||
t.is(subtract(0, 0), 0);
|
||||
t.is(subtract(4, 2), 2);
|
||||
});
|
||||
7
examples/typescript-basic/tsconfig.json
Normal file
7
examples/typescript-basic/tsconfig.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"extends": "@sindresorhus/tsconfig",
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"outDir": "build",
|
||||
}
|
||||
}
|
||||
21
examples/typescript-context/package.json
Normal file
21
examples/typescript-context/package.json
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "ava-typescript-context",
|
||||
"description": "TypeScript example for typing t.context",
|
||||
"scripts": {
|
||||
"test": "ava"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ava/typescript": "^2.0.0",
|
||||
"@sindresorhus/tsconfig": "^1.0.2",
|
||||
"ava": "^3.15.0",
|
||||
"typescript": "^4.3.4"
|
||||
},
|
||||
"ava": {
|
||||
"typescript": {
|
||||
"compile": "tsc",
|
||||
"rewritePaths": {
|
||||
"source/": "build/"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
5
examples/typescript-context/readme.md
Normal file
5
examples/typescript-context/readme.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# TypeScript context example
|
||||
|
||||
> TypeScript example for [typing `t.context`](https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md#typing-tcontext)
|
||||
|
||||
[](https://stackblitz.com/github/avajs/ava/tree/main/examples/typescript-context?file=source%2Ftest.ts&terminal=test&view=editor)
|
||||
1
examples/typescript-context/source/index.ts
Normal file
1
examples/typescript-context/source/index.ts
Normal file
|
|
@ -0,0 +1 @@
|
|||
export const concat = (input: string[]) => input.join(' ');
|
||||
15
examples/typescript-context/source/test.ts
Normal file
15
examples/typescript-context/source/test.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import anyTest, {TestInterface} from 'ava';
|
||||
|
||||
import {concat} from '.';
|
||||
|
||||
const test = anyTest as TestInterface<{sort: (a: string, b: string) => number}>;
|
||||
|
||||
test.beforeEach(t => {
|
||||
t.context = {
|
||||
sort: (a: string, b: string) => a.localeCompare(b)
|
||||
};
|
||||
});
|
||||
|
||||
test('concat', t => {
|
||||
t.is(concat(['b', 'c', 'a'].sort(t.context.sort)), 'a b c');
|
||||
});
|
||||
7
examples/typescript-context/tsconfig.json
Normal file
7
examples/typescript-context/tsconfig.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"extends": "@sindresorhus/tsconfig",
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"outDir": "build",
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue