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
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.
6
6
7
+
If you use TypeScript with `ts-node` please [see our Typescript recipe for setup instructions](./typescript.md).
8
+
7
9
## Using the `esm` package
8
10
9
11
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`.
Copy file name to clipboardExpand all lines: docs/recipes/typescript.md
+63-3Lines changed: 63 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,73 @@ 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
12
+
11
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.
12
14
13
15
### Using `ts-node`
14
16
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
+
exportfunction 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"
16
76
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:
18
78
19
79
`package.json`:
20
80
@@ -31,7 +91,7 @@ You can use [`ts-node`] to do live testing without transpiling to js files. Thi
31
91
}
32
92
```
33
93
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`.
0 commit comments