Skip to content
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 loading ESM test files #2382

Merged
merged 26 commits into from
Feb 9, 2020
Merged

Support loading ESM test files #2382

merged 26 commits into from
Feb 9, 2020

Conversation

arlac77
Copy link
Contributor

@arlac77 arlac77 commented Jan 26, 2020

Fixes #2344.

Brute force try import(ref)
at least works for me

How to make xo not complain about fully qualified imports ?

import test from '../../index.js';

@sindresorhus
Copy link
Member

You need to await the import call. Otherwise, the try/catch has no effect.

@arlac77
Copy link
Contributor Author

arlac77 commented Jan 27, 2020

You need to await the import call. Otherwise, the try/catch has no effect.

the try catch is there to prevent the unknown import() function error (node < 10?)
and fall through to the former error handling

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Big picture the current error message needs to be updated for when ESM is not available, cause with this PR AVA does support loading ESM test files.

It'd be good to have a test for when ESM does work, too.

test/fixture/esm/test.js Outdated Show resolved Hide resolved
lib/worker/subprocess.js Outdated Show resolved Hide resolved
test/integration/assorted.js Outdated Show resolved Hide resolved
@novemberborn novemberborn changed the title fix: use dynamic import() to load esm modules Support loading ESM test files Jan 27, 2020
@novemberborn
Copy link
Member

How to make xo not complain about fully qualified imports ?

import test from '../../index.js';

The XO output should contain the failing rule. Then after that line add // eslint-disable-line the-failing-rule and the rule should be ignored at that point.

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @arlac77!

lib/worker/subprocess.js Outdated Show resolved Hide resolved
test/integration/assorted.js Outdated Show resolved Hide resolved
@novemberborn
Copy link
Member

@arlac77 great find with the path vs URL stuff!

I think there's two remaining issues:

  • When import() is not available, the error message shown should communicate that. Currently it says AVA doesn't support ESM.
  • Errors thrown in the imported module should be reported, like they are for CJS modules.

@arlac77
Copy link
Contributor Author

arlac77 commented Feb 2, 2020

  • like they are for CJS modules.

Where can I find CJS module error code ?

@novemberborn
Copy link
Member

  • like they are for CJS modules.

Where can I find CJS module error code ?

No, the issue is the catch {} statement. You're assuming the only thrown error is because Node.js does not support ESM. But you're inadvertently swallowing errors thrown by the module.

Instead I think the safest way to deal with this is to explicitly detect ESM support by importing a known .mjs file included with AVA itself. Then in the load() function you can check for support before calling import(). If it's missing, emit the internal error and exit. Otherwise import without swallowing errors.

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also update https://github.com/avajs/ava/blob/8fa28254dbebef32cbde05c0c9a49061d0ef82f8/docs/recipes/es-modules.md which currently states that ESM files cannot be loaded.

lib/worker/subprocess.js Outdated Show resolved Hide resolved
lib/worker/subprocess.js Outdated Show resolved Hide resolved
lib/worker/subprocess.js Show resolved Hide resolved
lib/worker/subprocess.js Outdated Show resolved Hide resolved
@novemberborn
Copy link
Member

Thanks @arlac77. This looks good at first glance, but it'll probably take until the weekend before I can take a closer look.

@novemberborn
Copy link
Member

@arlac77 this is great! I've made some tweaks and updated the documentation. Will merge when CI passes 🎊

@novemberborn novemberborn merged commit 04ba44b into avajs:master Feb 9, 2020
let requireFn = require;
const load = async ref => {
for (const extension of extensionsToLoadAsModules) {
if (ref.endsWith(`.${extension}`)) {
ipc.send({type: 'internal-error', err: serializeError('Internal runner error', false, new Error('AVA cannot yet load ESM files.'))});
if (await supportsESM()) { // eslint-disable-line no-await-in-loop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think

if (cachedSupportsESMResult || await supportsESM())

can make this loop faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support loading .mjs test files
4 participants