dirv / svelte-test-register

Compile imported Svelte 3 components on Node (e.g. for test runners like Mocha and Jasmine)
MIT License
2 stars 0 forks source link

Error when running with mocha with ES6 modules #1

Open asm0dey opened 4 years ago

asm0dey commented 4 years ago

I'm trying to use mocha as it's written in readme and getting following error:

$ mocha -r svelte-test-register

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".svelte" for /home/finkel/work_self/svelte/my-svelma-project/src/App.svelte
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:113:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:244:31)
    at async ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:44:17)
    at async Promise.all (index 0)
    at async link (internal/modules/esm/module_job.js:48:9)
error Command failed with exit code 1.
dirv commented 4 years ago

Hi, thanks for raising this. Judging by the stack trace, you’re using Node with ES6 module support, which is great to see but unfortunately, Node doesn’t yet have a complete mechanism for hooking into the import statement.

You have some options:

  1. Turn off ES6 module loading for your tests, and use Babel to transpile your tests. If you’re using Rollup you’ll need a separate config file to do that. There's an example of this approach in this repo. It’s not straightforward but I can help answer any questions.

  2. You can try using the new import.meta.resolve function, which is new in the latest version of Node, but I haven’t tried this myself and don’t feel too confident that it will work.

  3. If you’re feeling adventurous, you could try reimplementing this script using the experimental loader hooks. I’ve attempted this before and didn’t have any success, but it looks like the API has made some progress since then. There’s an example of a CoffeeScript loader here. This task is on my radar but I’d need to schedule this in, and again there’s no guarantee of success with the current state of the loader API.

For option 1, one way to turn off ES6 modules is to name your test file with a .cjs extension, but it depends on how you’ve set up your package.

Let me know if that helps!

asm0dey commented 4 years ago

Thank you for your answer! I'm aware of option 1, but it eliminates speed bonuses of jasmine and should make is as slow as jest is. Other two options look really creepy to me :)

dirv commented 4 years ago

Let’s keep this issue open until we have a satisfactory solution for test files that use Node’s ES6 module support.

0gust1 commented 4 years ago

Maybe a silly lead (I have similar concerns) :

Use mocha --require @babel/register to launch the tests, and in your test file, use import 'svelte/register'; ?

On my side, I still struggle to make the aliases inside my svelte component work, but the component in itself is well imported.

0gust1 commented 4 years ago

Seems not so silly : if I don't use aliases in my modules, it works ! Now, let's take a look at https://github.com/tleunen/babel-plugin-module-resolver for aliases handling.

dirv commented 4 years ago

Nice idea, I guess the downside is that you’d need to include that in every test file?

Keep us posted on the alias issue :)