Mocking electron modules
See original GitHub issueIs 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:
- Created 7 years ago
- Reactions:5
- Comments:15 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Given this file structure.
This is what I did to get it right.
args: [path.join(__dirname, '..'), '-r', path.join(__dirname, 'mocks.js')]
The first argument correspond to the path of your main.js file. (wherever that may be)
The second arguments corresponds to the require statement to preload with
-r
your files (in your case, your mocks). i.eelectron main.js -r test/mocks.js
should work in your console if you have electron installed globally.The 3rd corresponds to your mocks path. (edited)
In main.js I did the following.
In my mock
~ Sadly it took me 3 hours to find, I hope this helps anyone else.
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 theargs
array and preload the mocks that way to no pollute your regular app code.