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.

Effective testing for IPC events?

See original GitHub issue

I’m trying to find a way of effectively testing for IPC events being sent/received on either the main or renderer processes.

Looking at the spectron tests, I came across this: https://github.com/electron/spectron/blob/master/test/application-test.js#L219. However, this approach of assigning a global variable within the main process seems awkward. It also requires modifying the application’s code just to enable the test capability for any given IPC event.

I tried the following to no avail:

it("listens for a message on ipcRenderer", function(done) {
    app.client.waitUntilWindowLoaded().then(() => {
      const ipcRenderer = app.electron.ipcRenderer;

      ipcRenderer.on('msg-received', function(evt, payload) {
        payload.should.be("SUCCESS")
        done()
      })

      ipcRenderer.send("msg-received", "SUCCESS")
    })
  });

Evens sent directly from within the renderer process also won’t be captured by tests, and I suspect it’s because mocha’s test process is separate from the process running Electron.

So, is it possible to receive Electron events from either process within a Spectron test, in a similar manner to the example above? I suspect not, but if this is the case, what can be done to enable this capability?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
MarshallOfSoundcommented, Jul 21, 2016

ipcRenderer does not send messages to itself, it sends them to ipcMain. I haven’t tried this in spectron yet but you could probably do this.

it("listens for a message on ipcRenderer", function(done) {
    app.client.waitUntilWindowLoaded().then(() => {
      app.electron.remote.ipcMain.on('msg-received', function(evt, payload) {
        payload.should.be("SUCCESS")
        done()
      })

      app.electron.ipcRenderer.send("msg-received", "SUCCESS")
    })
  });

However this raises a point about testing as a whole. Considering the IPC framework that electron uses is thoroughly tested. See https://github.com/electron/electron/blob/master/spec/api-ipc-spec.js

Do you actually want / need to test that IPC works correctly. What you need to test is that given a function is called with the correct parameters the correct action occurs. For example.

// This is what you need to test
const doThing = (event, data) => {
  // Do the thing
};

// This is not
ipcRenderer.on('thing', doThing);

You should assume that the framework you are using (Electron) is tested correctly and test your code does what it should, not that IPC does what it should.

This is all just IMO but it makes sense to me 😆

3reactions
paulius005commented, Nov 7, 2018

@Aerlinger did you ever find a solution for listening for the application’s IPC events?

@MarshallOfSound listening with app.electron.remote.ipcMain.on does not seem to return any events. We have architected our app with electron-redux and which sends redux actions through IPC.

Being able to listen to ipc would enable our e2e testing to be much more precise when it comes to waiting for certain actions to complete.

The only work around that I can think of here is to spin up a small websocket to communicate between the two processes, but that’s a heavy solution to a problem that seems just out of reach on this end, as I can send IPC events to the application without issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Effective testing for IPC events? · Issue #91 · electron ... - GitHub
I'm trying to find a way of effectively testing for IPC events being sent/received on either the main or renderer processes.
Read more >
What is IPC Testing? | National Technical Systems - NTS
NTS has been accredited to perform IPC testing since 1980 originally by the military and later by A2LA (American Association for Laboratory Accreditation) ......
Read more >
Testing the IPC Protocol for a Real-Time Operating System
Abstract In this paper, we adapt model-based testing techniques to concurrent code, namely for test generations of an (industrial) OS kernel called PikeOS....
Read more >
Measurement Precision Estimation for Binary Data - ipc.org
The precision of the test is determined by calculating the consistency and correctness of the sample dispositions. Measurements that result in variables data ......
Read more >
Instrument Proficiency Check (IPC) Guidance
effective way to test a pilot's knowledge in the context of real-world IFR flying, so consider using the pre-assigned XC flight plan as...
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