question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support to Run TypeScript test file(.ts) directly without Precompiling(tsc)

See original GitHub issue

Issuehunt badges

Description

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.

ava-typescript

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.

  1. 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, like ava -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 by ava test.ts

  2. 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 the ext parameter. I use exts in the ava-files module and it is an Array type, that’s for the future support of multiple file extensions.

  3. new API options: ext

    in API call, we get file extension name from options.ext, pass it to AvaFiles/CachingPrecompiler/Watcher.

  4. 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 to ts, then CachingPrecompiler will use a new module extTs to compile the source.

  5. Introduce a new Module: extTs(lib/ext/ts.js)

    For ts file extension, the compiler module ext/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 new extJsx compiling module.

  6. Enable Run TypeScript in test-worker.js

    AVA use subprocess to run tests, the worker needs to register to TypeScript compiler.

  7. Add TypeScript dependence to package.json

  8. 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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:114
  • Comments:75 (31 by maintainers)

github_iconTop GitHub Comments

20reactions
novemberborncommented, Aug 19, 2018

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 requiring ts-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.

20reactions
andywercommented, Oct 29, 2017

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 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

avajs/ava: Support to Run TypeScript test file(.ts) directly ...
AVA now support ES6 by using Babel, run directly without any precompiling, ... But for TypeScript, AVA has to precompile ts files by...
Read more >
How do I run TypeScript files directly without generating any ...
will run typescript without node example.js command How can I do it. You can use ts-node to compile + run directly (without ever...
Read more >
ts-node - npm
It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node.js without precompiling.
Read more >
Switching from Ava to Jest for TypeScript | by Gant Laborde
This epic Github issue ensued. Support to Run TypeScript test file(.ts) directly without Precompiling(tsc) · Issue #1109 · avajs…
Read more >
Use TypeScript to Build a Node API with Express
VS Code has exceptional support for JavaScript and Node.js, such as smart code completion ... ts-node, Use to run TypeScript files directly.
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found