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
Update TypeScript recipe to remove ts-node recommendation (#3192)
* Update typescript recipe to use the tsx loader;
* Remove ts-node references; strongly advocate @ava/typescript; general instructions for setting up custom loaders
---------
Co-authored-by: Mark Wubben <[email protected]>
@@ -8,92 +8,33 @@ This guide assumes you've already set up TypeScript for your project. Note that
8
8
9
9
## Enabling AVA's support for TypeScript test files
10
10
11
-
### With precompile step
11
+
Broadly speaking, there are two ways to run tests written in TypeScript:
12
12
13
-
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.
13
+
1. Build first, then test against the build output
14
+
2. Configure loaders which build test files as they're loaded
14
15
15
-
### Using `ts-node`
16
+
**The first option is the most reliable since it doesn't rely on experimental Node.js features.** 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. **This package also sets up the various TypeScript file extensions for you.**
16
17
17
-
You can use [`ts-node`] to do live testing without transpiling. This can be especially helpful when you're using a bundler. Be sure to install the required dev dependencies:
18
+
**You can use loaders, but you're largely on your own. [Please post questions to our Discussions forum if you're stuck](https://github.com/avajs/ava/discussions/categories/q-a).**
18
19
19
-
`npm install --save-dev typescript ts-node`
20
+
There are two components to a setup like this:
20
21
21
-
The required setup depends on the type of your package:
22
+
1.[Make sure AVA recognizes the extensions of your TypeScript files](../06-configuration.md#configuring-module-formats)
23
+
2. Install the loader [through `nodeArguments`](../06-configuration.md#node-arguments)
22
24
23
-
1.[for packages with type "module"](#for-packages-with-type-module)
24
-
2.[for packages without type "module"](#for-packages-without-type-module)
25
-
26
-
#### For packages with type `module`
27
-
28
-
If your `package.json` has `"type": "module"`, then this is the AVA configuration you need:
29
-
30
-
`package.json`:
31
-
32
-
```json
33
-
{
34
-
"ava": {
35
-
"extensions": {
36
-
"ts": "module"
37
-
},
38
-
"nodeArguments": [
39
-
"--loader=ts-node/esm"
40
-
]
41
-
}
42
-
}
43
-
```
44
-
45
-
You also need to have this in your `tsconfig.json`:
46
-
47
-
```json
48
-
{
49
-
"compilerOptions": {
50
-
"module": "ES2020",
51
-
"moduleResolution": "node"
52
-
}
53
-
}
54
-
```
55
-
56
-
Remember that, by default, ES modules require you to specify the file extension and TypeScript outputs `.js` files, so you have to write your imports to load from `.js` files not `.ts`.
57
-
58
-
If this is not to your liking there is an _experimental_ option in Node.js that you might want to use. You can add it to the `nodeArguments` array in the AVA configuration so it applies to your test runs: [`--experimental-specifier-resolution=node`](https://nodejs.org/api/esm.html#customizing-esm-specifier-resolution-algorithm).
59
-
60
-
#### For packages without type "module"
61
-
62
-
If your `package.json` does not have `"type": "module"`, then this is the AVA configuration you need:
63
-
64
-
`package.json`:
65
-
66
-
```json
67
-
{
68
-
"ava": {
69
-
"extensions": [
70
-
"ts"
71
-
],
72
-
"require": [
73
-
"ts-node/register"
74
-
]
75
-
}
76
-
}
77
-
```
78
-
79
-
It's worth noting that with this configuration, tests will fail if there are TypeScript build errors. Suppose you want to test while ignoring these errors. In that case, you can use `ts-node/register/transpile-only` instead of `ts-node/register` or add an environment variable for ts-node to log errors to stderr instead of throwing an exception.
25
+
[`tsx`](https://github.com/esbuild-kit/tsx) may be the best loader available. The setup, assuming your TypeScript config outputs ES modules, would look like this:
80
26
81
27
`package.json`:
82
28
83
29
```json
84
-
{
85
-
"ava": {
86
-
"extensions": [
87
-
"ts"
88
-
],
89
-
"environmentVariables": {
90
-
"TS_NODE_LOG_ERROR": "true"
91
-
},
92
-
"require": [
93
-
"ts-node/register"
94
-
]
95
-
}
96
-
}
30
+
"ava": {
31
+
"extensions": {
32
+
"ts": "module"
33
+
},
34
+
"nodeArguments": [
35
+
"--loader=tsx"
36
+
]
37
+
}
97
38
```
98
39
99
40
## Writing tests
@@ -236,47 +177,4 @@ test('throwsAsync', async t => {
236
177
237
178
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.
238
179
239
-
### Using module path mapping
240
-
241
-
`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).
242
-
243
-
Once installed, add the `tsconfig-paths/register` entry to the `require` section of AVA's config:
0 commit comments