Add StackBlitz examples for TypeScript

This commit is contained in:
Sam Verschueren 2021-08-07 15:11:27 +02:00 committed by GitHub
parent 58150a0c9e
commit a898154010
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 104 additions and 1 deletions

View file

@ -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",

View file

@ -95,6 +95,8 @@ It's worth noting that with this configuration, tests will fail if there are Typ
## Writing tests
[![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)
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)
[![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)
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

View 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/"
}
}
}
}

View 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)
[![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)

View file

@ -0,0 +1,3 @@
export const sum = (a: number, b: number) => a + b;
export const subtract = (a: number, b: number) => a - b;

View 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);
});

View file

@ -0,0 +1,7 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"module": "commonjs",
"outDir": "build",
}
}

View 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/"
}
}
}
}

View 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)
[![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)

View file

@ -0,0 +1 @@
export const concat = (input: string[]) => input.join(' ');

View 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');
});

View file

@ -0,0 +1,7 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"module": "commonjs",
"outDir": "build",
}
}