Skip to content

Commit a079152

Browse files
mesqueebsindresorhusnovemberborn
authored
Improve TypeScript recipe for ESM and ts-node usage
Co-authored-by: Sindre Sorhus <[email protected]> Co-authored-by: Mark Wubben <[email protected]>
1 parent 61ded90 commit a079152

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

docs/recipes/es-modules.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/do
44

55
As of Node.js 12.17, [ECMAScript Modules](https://nodejs.org/docs/latest/api/esm.html#esm_introduction) are natively supported in Node.js itself. AVA 3.3 supports ESM test files, however support is incomplete. The [ESM support project](https://github.com/orgs/avajs/projects/2) tracks our progress.
66

7+
If you use TypeScript with `ts-node` please [see our Typescript recipe for setup instructions](./typescript.md).
8+
79
## Using the `esm` package
810

911
If you're using Node.js 10 and AVA 3 and you want to use the ESM syntax, without relying on Node.js' implementation, your best bet is to use the [`esm`](https://github.com/standard-things/esm) package. Make sure to use the `.js` extension and *do not* set `"type": "module"` in `package.json`.

docs/recipes/typescript.md

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,73 @@ 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
12+
1113
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.
1214

1315
### Using `ts-node`
1416

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.
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+
19+
`npm install --save-dev typescript ts-node`
20+
21+
Then, depending on whether or not your package is of type `module` or not, the required setup differs. See either:
22+
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+
"nonSemVerExperiments": {
39+
"configurableModuleFormat": true
40+
},
41+
"nodeArguments": [
42+
"--loader=ts-node/esm"
43+
]
44+
}
45+
}
46+
```
47+
48+
You also need to have this in your `tsconfig.json`:
49+
50+
```json
51+
{
52+
"compilerOptions": {
53+
"module": "ES2020",
54+
"moduleResolution": "node"
55+
}
56+
}
57+
```
58+
59+
And finally, even though you directly import code from your TypeScript files, you **must** import it from your `.ts` files with the `.js` extension instead!
60+
61+
For example if your source file is `index.ts` looks like this:
62+
63+
```ts
64+
export function myFunction() {}
65+
```
66+
67+
Then in your AVA test files you must import it **as if it has the `.js` extension** it like so:
68+
69+
```ts
70+
import {myFunction} from './index.js';
71+
```
72+
73+
The reason that you need to write `.js` to import `.ts` files in your AVA test files, is explained by the `ts-node` author [in this post](https://github.com/nodejs/modules/issues/351#issuecomment-621257543).
74+
75+
#### For packages without type "module"
1676

17-
`npm i --save-dev typescript ts-node`
77+
If your `package.json` does not have `"type": "module"`, then this is the AVA configuration you need:
1878

1979
`package.json`:
2080

@@ -31,7 +91,7 @@ You can use [`ts-node`] to do live testing without transpiling to js files. Thi
3191
}
3292
```
3393

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`.
94+
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`.
3595

3696
## Writing tests
3797

0 commit comments

Comments
 (0)