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

Provide option to enableRemoteModule or configure any webPreferences #45

Open
juanmartin opened this issue Nov 5, 2021 · 1 comment

Comments

@juanmartin
Copy link

Hi!
The project I'm currently working on needs to mock some remote.require we do on the angular side for some native modules we use.

import { remote } from 'electron';

/**
 * This mocks out main process modules that are incompatible with unit tests.
 */
const originalRequire = remote.require;
remote.require = jest.fn((moduleName) => {
  switch (moduleName) {
    // mock native module functions
    case './nativeModule':
      return () => true;
    // for all other modules, do not mock and return regular module
    default:
      return originalRequire(moduleName);
  }
});

Since the context in which Jest runs didn't include the needed enableRemoteModule: true web preference, as it's the default since Electron v10. Since we upgraded to v11, and still needed the remote module, our tests started failing.
I ended up doing a manual override in the postinstall script of our project to manually replace the JestWorkerRPC.js file with a copy of it in which I added enableRemoteModule: true.
Now our tests run and all is good. But I don't like to override a whole file within node_modules like this, so it would be nice to be able to configure certain aspects like the webPreferences of the BrowserWindow of the Jest Runner to avoid this kind of workaround.

I know the remote module is deprecated and it could be a security concern, but until we refactor our codebase, this is the solution we came up with.

Let me know if this is viable or anyone stumbled upon this case so that I can provide anything needed.

Thank you!

@davidrichard23
Copy link

hey @juanmartin would you mind posting an example of your postinstall script?

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