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

Add support for ESM #180

Closed
jarrodek opened this issue Jan 29, 2021 · 2 comments
Closed

Add support for ESM #180

jarrodek opened this issue Jan 29, 2021 · 2 comments

Comments

@jarrodek
Copy link

I upgraded my project to work as ESM. I use the esm module when running the main script, all my renderer classes are going through a custom protocol that load modules as defined in the ES spec. I am almost ready to release the app but apparently I can't perform tests because everything is crashes. So, similarly to mocha I tried to use --require esm which fixes the problem (electron-mocha uses --require-main or --require). This, however, didn't work. Then I tried a different approach which creates a a common export file just for test, where the main file is a regular Common JS file, but it employs esm module to load a ES file that has exports for the classes. this doesn't work as well. After that I don't really have more ideas how to proceed. Maybe you guys has a similar issue and can help here?

To visualize what I am trying to do.

@inukshuk
Copy link
Collaborator

It's not entirely clear yet how ESM is going to work in Electron, especially in the renderer process where there is a clash between Node's and Chromium's ESM loaders. I would prefer not to work on native ESM support in electron-mocha until the situation in Electron is clear.

That said, it's absolutely no problem to test apps using ESM with electron mocha if you can use a custom loader in between. I haven't used the esm module, but I've had no issues using babel to load ES modules into electron-mocha tests both in main and renderer. If esm hooks into require, similar as babel does, then it should be enough to add --require esm but your second approach should work as well. What's the error you're running into?

@jarrodek
Copy link
Author

Hi,
Sorry for inaccuracies. I was writing the issue report after the whole day of work in the middle of night :]

More or less I have figured out how to do this. Not sure if that's the most efficient way though.

In then test file I have the following in the imports:

const { assert } = require('chai');
const { session } = require('electron');
const _require = require('esm')(module);

const { MyClass } = _require('../../src/io/MyClass.js');

This way I am able to import a class that works in the main process for unit testing. This also works for renderer classes. Types won't work this way but at least I am able to run tests again.

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

No branches or pull requests

2 participants