diff --git a/docs/recipes/typescript.md b/docs/recipes/typescript.md index 90b8d1f4c..84c1516e3 100644 --- a/docs/recipes/typescript.md +++ b/docs/recipes/typescript.md @@ -4,15 +4,34 @@ Translations: [EspaƱol](https://github.com/avajs/ava-docs/blob/master/es_ES/doc AVA comes bundled with a TypeScript definition file. This allows developers to leverage TypeScript for writing tests. -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. - This guide assumes you've already set up TypeScript for your project. Note that AVA's definition expects at least version 3.7.5. -## Enabling AVA's TypeScript support +## Enabling AVA's support for TypeScript test files + +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. + +### Using `ts-node` + +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. + +`npm i --save-dev typescript ts-node` -Currently, AVA's TypeScript support is designed to work for projects that precompile TypeScript. Please see [`@ava/typescript`] for setup instructions. +`package.json`: + +```json +{ + "ava": { + "extensions": [ + "ts" + ], + "require": [ + "ts-node/register" + ] + } +} +``` -Read on until the end to learn how to use [`ts-node`] with AVA. +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`. ## Writing tests @@ -153,27 +172,6 @@ test('throwsAsync', async t => { 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. -## On the fly compilation using `ts-node` - -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`]: - -`package.json`: - -```json -{ - "ava": { - "extensions": [ - "ts" - ], - "require": [ - "ts-node/register" - ] - } -} -``` - -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`. - ### Using module path mapping `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).