Since all of my project files are *.mjs, is it possible that AVA supports native ESM? Or can I add additional node parameter "--experimental-modules" to AVA?
Whilst support remains experimental we won't support it directly, no. You can use AVA with the esm package though, see https://github.com/avajs/ava/blob/master/docs/recipes/es-modules.md.
That said perhaps we should support a way of passing options to the worker processes. We could add --node-args CLI flag, and support nodeArgs in AVA's configuration files. What do you think?
Would it also require the restriction to only process *.js files to be relaxed?
@PhilT yes but that's landing soon (#1746).
I imagine AVA should use import() to load the test file too. That'll be trickier, since we currently expect a synchronous require('ava'). Still, our esm-the-package support means there's a way to hack into that process:
Hi novemberborn, thank you very much for the reply. The --node-args CLI flag or the corresponding config object is exactly what we need. I'm OK that AVA only processes *.js file. But I do need AVA support the statement like "import xxx from './yyy.mjs'" natively. Because it's a lot of extra work to introduce babel in test.
By the way, the esm package works well. Thank you again.
@jim-king-2000 configurable extension support has landed, so AVA can now run .mjs files. You can disable AVA's compilation of test files, or disable just the conversion of ESM to CJS.
I think the one remaining question is how AVA would actually load the files. I don't think it can use require() with --experimental-modules.
@novemberborn Fantastic! I tried the latest beta5 AVA and it loaded my test_*.mjs successfully. My config file is like the following. No babel any more.
"ava": {
"require": [
"esm"
],
"babel": false,
"extensions": [
"mjs"
]
},
@novemberborn I don't know how to implement it, however, I'd really like to digest "test_*.mjs" with empty config file or with a little bit more characters as the following:
"ava": {
"node-args": [ "--experimental-modules" ]
}
I could make a ticket for the nodeArgs option, but that won't help with --experimental-modules.
We should also extend AVA's handling of esm so alternative loaders can be configured, including supporting asynchronous loaders. Then somebody could publish a package that wraps import() so it works with AVA.
Leaving this open for somebody to spec this out further.
It seems that AVA has it's own module load mechanism. So, when es module becomes the fully supported feature by node and can be used without the "--experimental-modules" argument, AVA still requires "esm" to be installed. Is it right?
So, when es module becomes the fully supported feature by node and can be used without the "--experimental-modules" argument, AVA still requires "esm" to be installed. Is it right?
@jim-king-2000 we'll follow the default behavior in Node.js.
@novemberborn Great! I think it would be soon for node to support es6 module officially. Then AVA would work without installing "esm" module. Thank you for the help all along.
Hi @novemberborn , long time no see. Shall we let AVA run *.mjs without "esm" plug-in now? The "esm" plug-in can work, but the error reported by AVA is based to the transpiled code which is hard for us to find the true culprit in our source code.
We won't support it directly while it's behind a flag, but I'd be happy to take PRs that allow AVA to opt-in to experimental Node.js features, if controlled through the appropriate flags.
What bothers us mostly is the line of the errors reported by AVA is NOT the line in our source code when using -r esm. How can we (or AVA) circumvent this issue? Shall we add some more plug-ins (which I don't know) to "magically" map the transpiled line to the original line in the error reports? Or should AVA add an experimental flags as nodejs does (usage becomes this: ava --experimental-modules ./test/*.mjs)?
@jim-king-2000 this discussion may be relevant: https://github.com/avajs/ava/issues/1980
There's a lot of different tools interacting here. I don't fully understand what's going wrong either.
Again I'd be happy to find a way so you can configure experimental Node.js flags and have them apply to AVA's worker processes.
Hi @novemberborn , I'm so sorry that I haven't made a minimal repro for the issue I reported. Now when I'm trying to do it, I find that it is not reproduced. So, it is almost perfect of AVA at this time. I'm still looking forward to the experimental flag(s), which would make it absolutely perfect.
Came here after the Node 12 release announced the anticipating dropping of the --experimenta-modules flag in October.
I was hoping I'd be able to run my .mjs tests without Babel, since Jest can't do that. Can Ava already somehow, or do we need #2090 solved first?
In package.json, I have:
"ava": {
"babel": false,
"compileEnhancements": false,
"extensions": [
"mjs"
]
}
@dandv for now, I'd like to solve this through #2090. Closer to October we can get AVA ready to support ESM natively.
By specifying the NODE_OPTIONS environment variable, #2112 should also do the trick, whichever gets released first.
Do I understand it correctly, that for as long as node_modules/ava/cli.js is a common js module, all test files and code under test needs to be transformed from es modules to common js modules?
@dandv for now, I'd like to solve this through #2090. Closer to October we can get AVA ready to support ESM natively.
Any news after Node v13.2.0 dropped the --experimental flag for ES modules?
@dimaqq @dandv please see https://github.com/avajs/ava/issues/2293.
Most helpful comment
@novemberborn Fantastic! I tried the latest beta5 AVA and it loaded my test_*.mjs successfully. My config file is like the following. No babel any more.