Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

AVA with --experimental-modules #1810

Closed
jim-king-2000 opened this issue May 25, 2018 · 23 comments
Closed

AVA with --experimental-modules #1810

jim-king-2000 opened this issue May 25, 2018 · 23 comments

Comments

@jim-king-2000
Copy link

jim-king-2000 commented May 25, 2018

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?

@novemberborn
Copy link
Member

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?

@PhilT
Copy link

PhilT commented May 27, 2018

Would it also require the restriction to only process *.js files to be relaxed?

@novemberborn
Copy link
Member

@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:

if (required[Symbol.for('esm\u200D:package')]) {

@jim-king-2000
Copy link
Author

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.

@jim-king-2000
Copy link
Author

By the way, the esm package works well. Thank you again.

@novemberborn
Copy link
Member

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

@jim-king-2000
Copy link
Author

jim-king-2000 commented Jun 1, 2018

@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"
    ]
  },

@jim-king-2000
Copy link
Author

jim-king-2000 commented Jun 1, 2018

@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" ]
}

@novemberborn
Copy link
Member

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.

@novemberborn novemberborn changed the title AVA with esm. AVA with --experimental-modules Jun 4, 2018
@jim-king-2000

This comment has been minimized.

@novemberborn

This comment has been minimized.

@jim-king-2000

This comment has been minimized.

@jim-king-2000
Copy link
Author

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.

@novemberborn
Copy link
Member

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.

@jim-king-2000
Copy link
Author

jim-king-2000 commented Nov 20, 2018

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)?

@novemberborn
Copy link
Member

@jim-king-2000 this discussion may be relevant: #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.

@jim-king-2000
Copy link
Author

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.

@dandv
Copy link

dandv commented May 7, 2019

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"
    ]
  }

@novemberborn
Copy link
Member

@dandv for now, I'd like to solve this through #2090. Closer to October we can get AVA ready to support ESM natively.

@alexisfontaine
Copy link
Contributor

By specifying the NODE_OPTIONS environment variable, #2112 should also do the trick, whichever gets released first.

@dimaqq
Copy link

dimaqq commented Dec 10, 2019

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
Copy link

dandv commented Dec 10, 2019

@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?

@novemberborn
Copy link
Member

@dimaqq @dandv please see #2293.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

6 participants