avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Documentation Request: Using a Node Custom Loader #3286

Closed F1LT3R closed 10 months ago

F1LT3R commented 10 months ago

I really love ava, and I'm trying to build a case for using across all of our web projects at work.

But I'm running into an issue trying to use ava to test Web Components using relative imports as JavaScript Modules in the browser.

When I use ava I get the typical Node ESM Error:

[ERR_UNSUPPORTED_DIR_IMPORT]:
    Directory import '/home/user/my-project' is not supported resolving ES modules imported from ...
    /home/user/repos/my-other-project

I spent yesterday afternoon looking through your old bug reports, and your documentation on using ESM Loaders of various flavors, and I think the best way to go is using my own Node Custom Loader. Especially as there are special rules, such as: using Babel style imports, where extensions may be excluded for: .html, .js, .mjs; and conditionally serves index.html or index.mjs when hitting a directory /. (These are being handled by a custom http server).

What I can't find documented is how I would use my own custom loader with ava, assuming it's just a loader script running in my project.

Example of running my custom loader:

 node --loader ./loader.mjs web/components/my-component.mjs

Example of relative import from my-component.mjs:

import MyOtherModule from '../my-other-module'   // <-- loads '../my-other-module/index.mjs'

Perhaps there is a simple way to do this already, which I have missed?

I'm really just looking to use ava in a way that will respect a project level entry point to a loader.mjs that lives as a script within that same project, rather than using a node module name and package to handle loading.

If you guys are willing to help me get ava to work with a project based entry point to loader.mjs, I would be more than happy to be involved in any PRs, if any were necessary, and update your documentation myself.