Skip to content

Commit 20c66fe

Browse files
SamVerschuerensindresorhus
authored andcommitted
Close #589 PR: Add TypeScript recipe.
1 parent 5e02387 commit 20c66fe

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

docs/recipes/typescript.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# TypeScript
2+
3+
AVA comes bundled with a TypeScript definition file. This allows developers to leverage TypeScript for writing tests.
4+
5+
## Setup
6+
7+
First install the TypeScript compiler [tsc](https://github.com/Microsoft/TypeScript).
8+
9+
```
10+
$ npm install --save-dev tsc
11+
```
12+
13+
Create a [`tsconfig.json`](https://github.com/Microsoft/TypeScript/wiki/tsconfig.json) file. This file specifies the compiler options required to compile the project or the test file.
14+
15+
```json
16+
{
17+
"compilerOptions": {
18+
"module": "commonjs",
19+
"target": "es2015"
20+
}
21+
}
22+
```
23+
24+
Add a `test` script in the `package.json` file. It will compile the project first and then run AVA.
25+
26+
```json
27+
{
28+
"scripts": {
29+
"test": "tsc && ava"
30+
}
31+
}
32+
```
33+
34+
35+
## Add tests
36+
37+
Create a `test.ts` file.
38+
39+
```ts
40+
import test from 'ava';
41+
42+
async function fn() {
43+
return Promise.resolve('foo');
44+
}
45+
46+
test(async (t) => {
47+
t.is(await fn(), 'foo');
48+
});
49+
```
50+
51+
52+
## Execute the tests
53+
54+
```
55+
$ npm test
56+
```

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ AVA, not Ava or ava. Pronounced [`/ˈeɪvə/` ay-və](media/pronunciation.m4a?ra
699699
- [Endpoint testing](docs/recipes/endpoint-testing.md)
700700
- [When to use `t.plan()`](docs/recipes/when-to-use-plan.md)
701701
- [Browser testing](docs/recipes/browser-testing.md)
702-
702+
- [TypeScript](docs/recipes/typescript.md)
703703

704704
## Support
705705

0 commit comments

Comments
 (0)