Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access 'remote' module from unit tests

Tags:

electron

How do I access the 'remote' module or other main process modules from my unit tests? I'm getting Error: Cannot find module 'remote' when testing a component that depends on 'remote'. I've also looked into mocking it but am not sure how to handle that, any guidance would be much appreciated.

like image 688
slightlytyler Avatar asked Dec 27 '15 21:12

slightlytyler


2 Answers

So I didn't find a perfect answer in my search but I ended up just using mockery to mock the remote module. I also looked into electron-mocha but could never get it working though the readme leads me to believe it solves the issue I was having. If anyone has experience unit testing Electron apps that use React please post up a better solution!

like image 27
slightlytyler Avatar answered Oct 01 '22 18:10

slightlytyler


Accessing the remote module from your unit tests simply requires your tests to be running in the right context. To be specific, this would be in a Renderer process. You can do this by running your tests in a BrowserWindow.

It is likely that you'll want a more integrated workflow. For this electron-mocha has worked for me. Using it I split my tests into four separate tasks covering the different contexts I felt relevant:

  • Unit tests for main process code
  • Unit tests for render process code
  • Unit tests for react components (babel compilation)
  • Functional tests using Spectron

They have to be run independently and the start up time of electron is slower than you may be used to when using node.

Finding an answer to the mocking aspect of your question is why I'm here, but I'm expecting it to be the same as mocking any javascript object. I'll try and add to/edit this if I find a more specific solution.

like image 92
Simon Gregory Avatar answered Oct 01 '22 16:10

Simon Gregory