question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocking electron modules

See original GitHub issue

Is there any way to mock electron modules from spectron?

Currently, I have to add all my mocks to the main process directly in my application code. So my mocks get shipped with the app, which is far from ideal. I remove the code in my build process but it would be much better to have the mocks added by spectron instead.

My current setup looks something like the following:

tests/e2e.js

// ...
new Application({
  path: electronPath,
  env: { RUNNING_IN_SPECTRON: '1' },
  args: ['.'],
});
// ...

main.js

const electron = require('electron');
// ...
if (process.env.RUNNING_IN_SPECTRON) {
  electron.dialog.showOpenDialog = (opts, cb) => {
    if (opts.title === 'Choose Site Folder') {
      cb(['/Users/dave/lorem/ipsum/My Site']);
    }
  };
}
// ...

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
etiennejcharlescommented, Jul 28, 2021

Given this file structure.

image

This is what I did to get it right.

args: [path.join(__dirname, '..'), '-r', path.join(__dirname, 'mocks.js')]

  1. The first argument correspond to the path of your main.js file. (wherever that may be)

  2. The second arguments corresponds to the require statement to preload with -r your files (in your case, your mocks). i.e electron main.js -r test/mocks.js should work in your console if you have electron installed globally.

  3. The 3rd corresponds to your mocks path. (edited)

In main.js I did the following.

//...
const mock = require('./test/mock')
//...
function createWindow () {
  if(process.env.RUNNING_IN_SPECTRON){
      mock(dialog);
  }
//...

In my mock

module.exports = function(dialog){
  dialog.showOpenDialog = (opts, cb) => {
    if (opts.title === 'Choose Site Folder') {
      cb(['/Users/dave/lorem/ipsum/My Site']);
    }
  }
}

~ Sadly it took me 3 hours to find, I hope this helps anyone else.

3reactions
kevinsawickicommented, Jul 25, 2016

Yeah, this isn’t currently possible since spectron only have access to run code in the renderer process and these APIs would need to be mocked in the main process.

One thing you could do now is add something like ['.', '-r', './tests/mocks.js'] to the args array and preload the mocks that way to no pollute your regular app code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I mock a method of a mocked module with Jest?
I am trying to mock the 'dialog' in electron module using jest and running into issues below. When setting up the mock directly...
Read more >
electron-mock-ipc - npm
Mock Electron's ipcMain and ipcRenderer. ... Start using electron-mock-ipc in your project by running `npm i electron-mock-ipc`.
Read more >
How to mock a dependency in a Node.js, and why you should ...
The only thing you have to tests — is a subject under test. As a good start — let's test simple module. The...
Read more >
Testing - Electron
Without these set, Electron will fail to perform some pre-testing steps. To run all unit tests, run npm run test . The unit...
Read more >
ECMAScript Modules - Jest
Module mocking in ESM​ ; const {BrowserWindow, app} = require('electron'); ; // etc. ; module.exports = {example};.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found