-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support to Run TypeScript test file(.ts) directly without Precompiling(tsc) #1109
Comments
Hi, Is there any feedback or suggestion? I want to know:
looking forward to getting feedback, then I can make the Pull Request fit better. |
Thanks for picking this up @zixia :) Your proposal looks pretty good. Some random feedback:
|
Got it. I'll follow your great feedback then make the PR later. :)
|
Hi @sindresorhus , I had followed all your feedbacks and made two PRs: one is for AvaFiles, the other is for AVA. Please notice that CI for AVA will be expected to report failure because the All unit tests had passed in my dev box with the PR code. Please let me know if I missed anything in PR, thanks. |
@zixia this is awesome work and I'm excited to use it, but can you tell me if it works fine with the |
@jedmao Hi buddy thanks you for like my work, and I'm sure you are a TypeScript fan like me. In theory, I think it should work with the If you want to try it before @sindresorhus merge this PR, you can pull my clone at https://github.com/zixia/ava (branch: typescript) , and also don't forget to link ava-files too: https://github.com/zixia/ava-files |
Is there any update on this? I'm starting a new TS project and I really would like to use ava without compiling TS scripts first! @zixia amazing work so far! |
@patrick91 Thanks! AVA decide to re-design the total code structure of how to use Babel, which delayed the TypeScript support progress. See: #1159 |
@patrick91 (and anyone else coming across this thread) – I've been getting into Typescript a lot recently, and had some trouble getting a lot of the packages I wanted to use working together. I spent a little time starter project which I think it might accomplish what you're looking for: es7-typescript-starter
I'd love input/feedback on how it could be improved, especially when AVA restarts work on this issue. |
@novemberborn I know I can do |
@lukescott you can add I think ava is super interesting but I have not replaced mocha over the typescript issue so the suggestion is not in use for me. |
Ah, got it. So I need need to run them separately. Tsc will watch and save files, which Ava will pick those up. It's too bad there isn't a way to do a single watcher and have it all happen in memory. |
Yep, I've been waiting for the same feature. |
The plan is here, but there hasn't been progress on that lately: https://github.com/avajs/ava/blob/master/docs/specs/001%20-%20Improving%20language%20support.md |
@lukescott for watch, you can do this: 'use strict';
const cp = require('child_process');
let ava;
cp.spawn('tsc', ['-w'], { shell: true })
.stdout.on('data', (data) => {
if (!ava) {
ava = cp.spawn('ava', ['-w'], {
stdio: 'inherit',
shell: true
})
}
const text = data.toString()
process.stdout.write(text)
if (/.*Compilation complete/.test(text)) {
let lint = cp.spawnSync('npm', ['run', 'lint'], {
stdio: 'inherit',
shell: true
})
if (lint.status === 0) {
cp.spawnSync('npm', ['run', 'build-commonjs'])
}
}
}) |
Has there been any work on this since then? |
babel has recently merged a PR that allows to parse typescript, so maybe soon enough we will be able to use ava for typescript via babel :) |
Looking forward it, or I have to consider other solutions like tap which had already supported the TypeScript(tapjs/tapjs#313) and Concurrency. |
If we can run TypeScript through Babel then yea we can land some sort of support for that. Though you'd still need the equivalent to |
With TypeScript support in babel, wouldn't babel-register (or what ever Ava uses) work? I would imagine babel adding the .ts extension w/ native support. |
I came to this thread/issue while trying to use Ava's --watch feature with TypeScript. @tomdavidson's suggestion of |
If anyone is interested, the solution @adieuadieu mentioned (from @tomdavidson) is what typescript-starter uses right now. |
I'm closing this issue since it's not directly actionable. Our plan for making AVA precompiler-agnostic can be found here: https://github.com/avajs/ava/blob/master/docs/specs/001%20-%20Improving%20language%20support.md — we'll need help achieving that. Further, we'd be willing to support TypeScript test files sooner if we can run them through our existing Babel infrastructure. |
It's worth mentioning that AVA 1.0 is now out 🎊. You can use typescript with this recipe. I've been using it in production for a while now, and it works great. This issue can be closed. |
@btkostner ts-node is not a drop-in replacement for IMO ava should be able to run this itself. |
The latest Jest 24 release supports TypeScript via @babel/preset-typescript |
@0maxxam0 has funded $2.00 to this issue.
|
@IssueHunt has funded $140.00 to this issue.
|
Should we close this and make a new issue that better summarizes the current state of affairs? Right now, the included recipe does a pretty-okay job of providing TypeScript support via ts-node. Current downsides are: As an alternative to |
@schmod Last time I chatted privately with @novemberborn about this, we discussed adding a config property like |
@sindresorhus – I'd certainly be happy with any of those options! |
ya, last i tried, a good debug experience still essentially requires that boilerplate (e.g. disabling compile enhancements) otherwise sourceMaps get real weird. i'm not sure i've tried w/ babel7 yet though, to be fair. disabling compile enhancements + ts-node still allow me to use breakpoints naturally in my editor and use the launch config recipes 👨🍳 without a headache. |
I'd like to get the Rollup org on Ava across the board, but TypeScript support is a major blocker. What's the chance that this is still something that's getting consideration? |
Just a question here. What's so wrong with pre-compiling? Why does a testing framework need to be concerned with TypeScript beyond providing types? Wouldn't it introduce more complexity into ava? |
Well, that was (partially) a secret of Jest's success. Would happily consider Ava if it has built-in TS support. If testing framework supports it - it automatically unlocks 1-click to run test in IDEs (e.g Jetbrains) which is a big DX win |
What about separation of concerns? Isn't that a DX win in the long term? |
My current thinking (once the extraction of Babel matures) is to support a TypeScript integration which knows how to load the pre-compiled files. So you specify I'm less convinced of the need to compile the entire project. What do folks think? |
Simply put: it's yet more friction for the adoption of Ava by TypeScript developers, where it doesn't exist elsewhere in other tools. As was mentioned by @kirillgroshkov, Jest has for a long time supported worriless, (relatively) frictionless TypeScript support. Yes, it would introduce more complexity. Supporting a larger user base tends to do that.
Agreed.
I would argue that DX is made worse. Separation of concerns works very well in many aspects of our trade, but this is not one.
Jest has really set the gold standard here. If there's different path from what Jest has done as a means to accomplish frictionless support of TypeScript, I'm all for it. But increased friction is a very hard sell for any team/org. I'd argue Ava is better than Jest in a myriad of ways, but forcing additional build steps is not appealing to folks. Config is generally fine, as even Jest requires being told about how it needs to hand TS (https://github.com/rollup/rollup-pluginutils/blob/master/jest.config.js)
Is there a timeline on this? If this is an unknown quantity, that's going to make for another point on a hard sell. |
I think that's outdated. If one is using babel with jest, everything is automagic, if babel is set-up correctly. Only when using What I want to say with that is, yes, setup is fine (in terms of friction), and it can be as easy as flipping a switch, as suggested above. |
@novemberborn, I really love that idea as a stop gap--it would make most of my projects just work, even if i had to turn off Babel and compile enhancements still. However, it does however have holes. First, many people compile to a different output dir, so a sibling is file isn't always an option. next, |
Regarding extracting Babel: the goal is for AVA to work with just Node.js, without any compilation. But, you'll be able to install As alluded to by others, you could use Babel to strip TypeScript. But I'd love to also have a separate I'm hoping to have a decent chunk of time to work on the Babel stuff, and other changes for an AVA 3 release as work breaks up for the holidays. |
Does this work with TS or not? |
Babel compilation has now been moved into a separate package, which means we can now do the same for TypeScript. See references to https://github.com/avajs/ava/blob/master/lib/babel-manager.js and beyond for ideas on how this might work. Ultimately this should be hosted at https://github.com/avajs/typescript so if you're keen to work on this give me a shout and I can get all that set up. |
Rudimentary support is now in progress at avajs/typescript#1. See also #2379. Example usage at novemberborn/got#1. This will go out in AVA 3.1; beyond that I'll create some issues in the https://github.com/avajs/typescript repository for where we can take this next. |
AVA 3.1 is now available: https://github.com/avajs/ava/releases/tag/v3.1.0 I've opened some issues to track enhancements here: https://github.com/avajs/typescript/issues Thank you all for your interest! |
Is there a TypeScript / ESM / Ava boilerplate repo? I'm having trouble getting it set up. |
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.
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 passext
to AVA API. If ext ists
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:
Run:
$ ava --ext ts test.ts ✔ AVA run TypeScript without tsc 1 test passed [02:55:58]
Yeah~
Relevant Links
.jsx
,.ts
) #631 (comment)The text was updated successfully, but these errors were encountered: