-
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
Update TypeScript recipe for ESM support #2593
Comments
Update Somehow I missed to also declare Trying to run TypeScript tests with ESM support, but failed to do so with the configuration above, adapted to my setup.
I'm running it with Node 14 and TypeScript 4, which results in this errors:
|
This comment has been minimized.
This comment has been minimized.
This comment was marked as outdated.
This comment was marked as outdated.
This doesn't work with default exports for me. e.g.
prints: @swc/core/Visitor looks like this:
|
@mikob that's a typical interoperability pattern to make CJS behave like ESM. However now that you're using actual ESM, it does not behave the way you expect. See https://nodejs.org/api/esm.html#esm_import_statements:
You might be able to |
Tried that as well without success. In the end I reverted to using "esm" (https://github.com/standard-things/esm) in the "requires" property for ava. |
That might paper over the cracks for now, yes. AVA 4 removes the special treatment for that package though. |
Node 16.10 or later will break the ESM loader, not sure if this affects this recipe. |
At this moment "ava": {
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--loader=esbuild-node-loader",
"--experimental-specifier-resolution=node"
],
"nonSemVerExperiments": {
"configurableModuleFormat": true
}
} |
The above config is much better. The default installation using I was also getting very odd failures. I narrowed down a couple problem tests and found that ES6 features that were working previously (in js before migrating to TypeScript) were behaving differently. Awaiting an object that wasn't a After switching to the above ( |
I created a small PR to update the typescript recipe per the above (I independently found the solution.. searching the issues here would have saved me some time :) ) |
I'm getting:
|
just started to work with AVA, and I want to replace Jest with it on my regular Webpack project (with Quasar Framework under the hood). I have a lot of No one guide helped me with setup because I have Typescript files written in ESM. I tried a few configs from issues here and from guides of AVA and But I do not want to compile tests before running, and I cannot add Only one working solution it using esbuild like @Akiyamka offers. Here's my working config: module.exports = {
'extensions': {
'ts': 'module',
},
'nodeArguments': [
'--loader=esbuild-node-loader',
'--experimental-specifier-resolution=node',
],
} Also I have to |
This comment was marked as outdated.
This comment was marked as outdated.
{
"extensions": {
"ts": "module"
},
"nodeArguments": [
"--loader=tsx",
]
} |
@novemberborn can you update the first post with @plantain-00’s and hide the outdated comments? Thankfully now the amount of config is reduced (tested on Node 18.13) Or maybe this issue should be closed since https://github.com/avajs/ava/blob/main/docs/recipes/typescript.md seems to be up to date (although it uses |
That works for me, though you're suggesting it's not ideal? |
Maybe including a link to this page would be helpful to determine which loader the user would want to use https://github.com/privatenumber/ts-runtime-comparison edit: I was looking at the original repo, not typestrongs |
@Sparticuz for the performance reasons tsx looks like a good option. But has no support for I agree that we should have a link for other possible loaders, so you could choose for a specific case. |
After several hours of head scratching and hair pulling trying to setup This should probably be added to the documentation as the current boilerplate does not work at all and always seems to error with:
|
A PR would be welcome! Personally I use https://github.com/avajs/typescript in my projects and compile TypeScript separately, which removes the dependency on various loaders and complications with the module systems. |
@novemberborn Does running ava on your compiled files affect coverage? I guess when I was compiling to cjs there were extra code paths that made it difficult to cover, but I'm not sure I've tried since switching to esm. |
@Sparticuz https://github.com/bcoe/c8 does the trick. |
The solution above worked for me up to nodejs/node version v19.9.0. Starting with node v20.0.0 (18/Apr/23) there are a lot of errors. TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" The workaround was to tweak the package.json as follows and start ava with
{
"scripts": {
"ava:run": "NODE_OPTIONS='--loader=tsx --no-warnings' ava"
},
"ava": {
"extensions": {
"ts": "module"
}
}
} |
Note that in the above, setting |
Node 20 changed experimental loaders. Fix running tests on node 20: * https://nodejs.org/en/blog/announcements/v20-release-announce * TypeStrong/ts-node#1997 * avajs/ava#2593 (comment)
See avajs/ava#2593 (comment) This fixes: ``` ./node_modules/.bin/ava (node:605416) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) Uncaught exception in tests/session_test.ts TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for code/websites/cards/tests/session_test.ts ✘ tests/session_test.ts exited with a non-zero exit code: 1 ─ 1 uncaught exception ```
I stumbled on this while searching about an issue with node + esm loading nothing to do with ava... Above there is a good suggestion for workaround to set the environment: NODE_OPTIONS='--loader=tsx --no-warnings' (make sure you have tsx installed as a dev dependency!) -- #2593 (comment) I wanted to add for the sake of anyone else who ends up here that |
@FallingSnow shared how to (experimentally) configure AVA and Node.js so that AVA can load TypeScript-based ESM files:
It'd be great to add this to our TypeScript recipe.
The text was updated successfully, but these errors were encountered: