Support to Run TypeScript test file(.ts) directly without Precompiling(tsc)
See original GitHub issueDescription
AVA now support ES6 by using Babel, run directly without any precompiling, this is very convenience. But for TypeScript, AVA has to precompile ts files by run tsc
.
I use TypeScript in my project wechaty, run TS code directly by ts-node
like this: ts-node demo.ts
. This is a convenience way for developing.
If we can run TypeScript file directly by AVA, just like ava test.ts
, that will be wonderful.
About this topic, there has already an issue #631 on it. I saw @sindresorhus agree that use TypeScript without precompiling is a better way.
@niieani You can already use TypeScript by precompiling it. I know it’s not optimal, but it’s not like anyone has done a pull request fixing this. https://github.com/avajs/ava/issues/631#issuecomment-250368210
I love AVA, because I’m a TAP fan, and AVA is based on TAP. So I tried to hack AVA to support TypeScript directly. After some study & work, I finished it finally, and in the past month, I was using it in my own project and it worked as my expected.
I’ll share my idea here, and send a Pull Request later. Hope we can support TypeScript without precompiling soon.
About the Hack
I added a --ext ts
argument parameter to CLI, then pass ext
to AVA API. If ext is ts
then I use TypeScript Compiler to replace the Babel Compiler, because TypeScript is the superset of ES6/7.
-
Add CLI Arg:
--ext
/-e
In order to support TypeScript without precompiling, first, we need to add a CLI args to support that. I use the name
--ext
and the alias-e
for this, likeava -e ts
. Set the ext automatically is considered, but I think maybe it should be done later. If set ext automatically, then we can just run TS tests byava test.ts
-
Get Test Files from
ava-files
AVA use a submodule
ava-files
to get all test files list. It had to be modified to support theext
parameter. I useexts
in the ava-files module and it is anArray
type, that’s for the future support of multiple file extensions. -
new API options:
ext
in API call, we get file extension name from
options.ext
, pass it toAvaFiles
/CachingPrecompiler
/Watcher
. -
Add a Switcher between Babel with TypeScript
AVA compile ES6 by Babel in
CachingPrecompiler
, so we could compile TypeScript here. If the file extension is set tots
, thenCachingPrecompiler
will use a new moduleextTs
to compile the source. -
Introduce a new Module:
extTs
(lib/ext/ts.js)For
ts
file extension, the compiler moduleext/ts.js
will be loaded. In the furture, other extension could modulize their own compile code in different file in ext/ directory. i.e.ext/jsx.js
for a newextJsx
compiling module. -
Enable Run TypeScript in
test-worker.js
AVA use subprocess to run tests, the worker needs to register to TypeScript compiler.
-
Add TypeScript dependence to package.json
-
Make XO happy
Test Source
test.ts:
import { test } from 'ava'
test('AVA run TypeScript without tsc', t => {
let i: number = 42
t.is(i, <number>42, 'meaning of life')
})
Run:
$ ava --ext ts test.ts
✔ AVA run TypeScript without tsc
1 test passed [02:55:58]
Yeah~
Relevant Links
There is a $142.00 open bounty on this issue. Add more on Issuehunt.
- Checkout the Issuehunt explorer to discover more funded issues.
- Need some help from other developers? Add your repositories on Issuehunt to raise funds.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:114
- Comments:75 (31 by maintainers)
Yes, AVA can now be configured to recognize the
.ts
file extension. With TypeScript 3 the build tooling has improved a lot, so I think we can make AVA compile a TypeScript project, without requiringts-node
.I’d like to port some of my own projects to TypeScript so I’d really like to see improved TypeScript support in AVA. For now though my priority is getting the 1.0 release out.
If anybody would like to help out with this project please give me a shout.
I know this is an old issue, but I needed the same thing, since a prior build step can be quite ugly.
So I quickly created ava-ts. Give it a try and leave feedback if you want 😃