Effective testing for IPC events?
See original GitHub issueI’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:
- Created 7 years ago
- Reactions:2
- Comments:13 (1 by maintainers)
Top GitHub Comments
ipcRenderer
does not send messages to itself, it sends them toipcMain
. I haven’t tried this in spectron yet but you could probably do this.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.
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 😆
@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