You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/06-configuration.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,8 @@ Note that providing files on the CLI overrides the `files` option.
61
61
62
62
Provide the `babel` option (and install [`@ava/babel`](https://github.com/avajs/babel) as an additional dependency) to enable Babel compilation.
63
63
64
+
Provide the `typescript` option (and install [`@ava/typescript`](https://github.com/avajs/typescript) as an additional dependency) to enable (rudimentary) TypeScript support.
65
+
64
66
## Using `ava.config.*` files
65
67
66
68
Rather than specifying the configuration in the `package.json` file you can use `ava.config.js` or `ava.config.cjs` files.
AVA comes bundled with a TypeScript definition file. This allows developers to leverage TypeScript for writing tests.
6
6
7
-
This guide assumes you've already set up TypeScript for your project. Note that AVA's definition has been tested with version 3.7.5.
8
-
9
-
## Configuring AVA to compile TypeScript files on the fly
10
-
11
-
You can configure AVA to recognize TypeScript files. Then, with `ts-node` installed, you can compile them on the fly.
12
-
13
-
`package.json`:
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.
14
8
15
-
```json
16
-
{
17
-
"ava": {
18
-
"extensions": [
19
-
"ts"
20
-
],
21
-
"require": [
22
-
"ts-node/register"
23
-
]
24
-
}
25
-
}
26
-
```
27
-
28
-
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`.
29
-
30
-
### Using module path mapping
31
-
32
-
`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).
33
-
34
-
Once installed, add the `tsconfig-paths/register` entry to the `require` section of AVA's config:
35
-
36
-
`package.json`:
37
-
38
-
```json
39
-
{
40
-
"ava": {
41
-
"extensions": [
42
-
"ts"
43
-
],
44
-
"require": [
45
-
"ts-node/register",
46
-
"tsconfig-paths/register"
47
-
]
48
-
}
49
-
}
50
-
```
51
-
52
-
Then you can start using module aliases:
53
-
54
-
`tsconfig.json`:
55
-
```json
56
-
{
57
-
"baseUrl": ".",
58
-
"paths": {
59
-
"@helpers/*": ["helpers/*"]
60
-
}
61
-
}
62
-
```
63
-
64
-
Test:
65
-
66
-
```ts
67
-
importmyHelperfrom'@helpers/myHelper';
68
-
69
-
// Rest of the file
70
-
```
71
-
72
-
## Compiling TypeScript files before running AVA
9
+
This guide assumes you've already set up TypeScript for your project. Note that AVA's definition has been tested with version 3.7.5.
73
10
74
-
Add a `test` script in the `package.json` file. It will compile the project first and then run AVA.
11
+
## Enabling AVA's TypeScript support
75
12
76
-
```json
77
-
{
78
-
"scripts": {
79
-
"test": "tsc && ava"
80
-
}
81
-
}
82
-
```
13
+
Currently, AVA's TypeScript support is designed to work for projects that precompile TypeScript. Please see [`@ava/typescript`] for setup instructions.
83
14
84
-
Make sure that AVA runs your built TypeScript files.
15
+
Read on until the end to learn how to use [`ts-node`] with AVA.
85
16
86
17
## Writing tests
87
18
@@ -221,3 +152,69 @@ test('throwsAsync', async t => {
221
152
```
222
153
223
154
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.
155
+
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
+
177
+
### Using module path mapping
178
+
179
+
`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).
180
+
181
+
Once installed, add the `tsconfig-paths/register` entry to the `require` section of AVA's config:
0 commit comments