Skip to content

Commit a3379fa

Browse files
SephReednovemberborn
andauthoredSep 27, 2020
Improve TypeScript recipe
Co-authored-by: Mark Wubben <mark@novemberborn.net>
1 parent df216e3 commit a3379fa

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed
 

‎docs/recipes/typescript.md

+24-26
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,34 @@ Translations: [Español](https://github.com/avajs/ava-docs/blob/master/es_ES/doc
44

55
AVA comes bundled with a TypeScript definition file. This allows developers to leverage TypeScript for writing tests.
66

7-
Out of the box AVA does not load TypeScript test files, however. Rudimentary support is available via the [`@ava/typescript`] package. You can also use AVA with [`ts-node`]. Read on for details.
8-
97
This guide assumes you've already set up TypeScript for your project. Note that AVA's definition expects at least version 3.7.5.
108

11-
## Enabling AVA's TypeScript support
9+
## Enabling AVA's support for TypeScript test files
10+
11+
Out of the box AVA does not load TypeScript test files. You can use our [`@ava/typescript`] package, which is designed to work for projects that precompile TypeScript using the `tsc` command. Please see [`@ava/typescript`] for setup instructions.
12+
13+
### Using `ts-node`
14+
15+
You can use [`ts-node`] to do live testing without transpiling to js files. This can be especially helpful when you're using a bundler.
16+
17+
`npm i --save-dev typescript ts-node`
1218

13-
Currently, AVA's TypeScript support is designed to work for projects that precompile TypeScript. Please see [`@ava/typescript`] for setup instructions.
19+
`package.json`:
20+
21+
```json
22+
{
23+
"ava": {
24+
"extensions": [
25+
"ts"
26+
],
27+
"require": [
28+
"ts-node/register"
29+
]
30+
}
31+
}
32+
```
1433

15-
Read on until the end to learn how to use [`ts-node`] with AVA.
34+
It's worth noting that with this configuration tests will fail if there are TypeScript build errors. If you want to test while ignoring these errors you can use `ts-node/register/transpile-only` instead of `ts-node/register`.
1635

1736
## Writing tests
1837

@@ -153,27 +172,6 @@ test('throwsAsync', async t => {
153172

154173
Note that, despite the typing, the assertion returns `undefined` if it fails. Typing the assertions as returning `Error | undefined` didn't seem like the pragmatic choice.
155174

156-
## On the fly compilation using `ts-node`
157-
158-
If [`@ava/typescript`] doesn't do the trick you can use [`ts-node`]. Make sure it's installed and then configure AVA to recognize TypeScript files and register [`ts-node`]:
159-
160-
`package.json`:
161-
162-
```json
163-
{
164-
"ava": {
165-
"extensions": [
166-
"ts"
167-
],
168-
"require": [
169-
"ts-node/register"
170-
]
171-
}
172-
}
173-
```
174-
175-
It's worth noting that with this configuration tests will fail if there are TypeScript build errors. If you want to test while ignoring these errors you can use `ts-node/register/transpile-only` instead of `ts-node/register`.
176-
177175
### Using module path mapping
178176

179177
`ts-node` [does not support module path mapping](https://github.com/TypeStrong/ts-node/issues/138), however you can use [`tsconfig-paths`](https://github.com/dividab/tsconfig-paths#readme).

0 commit comments

Comments
 (0)
Please sign in to comment.