Skip to content

Commit 49e5582

Browse files
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]>
1 parent 5bf01a3 commit 49e5582

File tree

1 file changed

+17
-119
lines changed

1 file changed

+17
-119
lines changed

docs/recipes/typescript.md

Lines changed: 17 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -8,92 +8,33 @@ This guide assumes you've already set up TypeScript for your project. Note that
88

99
## Enabling AVA's support for TypeScript test files
1010

11-
### With precompile step
11+
Broadly speaking, there are two ways to run tests written in TypeScript:
1212

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
1415

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.**
1617

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).**
1819

19-
`npm install --save-dev typescript ts-node`
20+
There are two components to a setup like this:
2021

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)
2224

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:
8026

8127
`package.json`:
8228

8329
```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+
}
9738
```
9839

9940
## Writing tests
@@ -236,47 +177,4 @@ test('throwsAsync', async t => {
236177

237178
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.
238179

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:
244-
245-
`package.json`:
246-
247-
```json
248-
{
249-
"ava": {
250-
"extensions": [
251-
"ts"
252-
],
253-
"require": [
254-
"ts-node/register",
255-
"tsconfig-paths/register"
256-
]
257-
}
258-
}
259-
```
260-
261-
Then you can start using module aliases:
262-
263-
`tsconfig.json`:
264-
```json
265-
{
266-
"baseUrl": ".",
267-
"paths": {
268-
"@helpers/*": ["helpers/*"]
269-
}
270-
}
271-
```
272-
273-
Test:
274-
275-
```ts
276-
import myHelper from '@helpers/myHelper';
277-
278-
// Rest of the file
279-
```
280-
281180
[`@ava/typescript`]: https://github.com/avajs/typescript
282-
[`ts-node`]: https://www.npmjs.com/package/ts-node

0 commit comments

Comments
 (0)