Open petef19 opened 2 years ago
Could you try an reproduce this with a minimal test case? We do handle the 'all-windows-closed' event specifically to allow tests to open and close windows. Is it possible that your code handles this event somewhere? For what it's worth, I'm also running window manager tests which open and close windows and have never experienced this issue.
@inukshuk
Thanks for the response.
Could you try an reproduce this with a minimal test case?
It's hard to share code bits because the code that runs the Electron
app is wrapped in a class, that does quite a few things. For example, the app
obj from Electron is passed into the class at instantiation and then assigned to a class var. But see further below for a simplified/reduced code example.
Is it possible that your code handles this event somewhere?
Yes, for example we do wrap app.on('before-quit')
, this.app.on('window-all-closed')
, <main_window>.on('closed')
- but all of these events then just call a class method, which then handles the tear down of the app. This works w/o issues when running the app.
This particular class method is stubbed (via sinon
) in the tests, but for whatever reason, when Electron
windows are open and then closed in e-mocha
tests, the sinon stub
on that method does not work... as I mentioned in the OP, stubbing/spying usually works just fine.
This is why we then tried to sinon stub
the app
obj itself, which also does not work consistently.
Any ideas what may be causing this behavior ?
a reduced/simplified code example:
class My_Electron_App{
constructor(...)
set_up(){
this.main_window.on('closed', async () => {
await this.teardown();
});
this.app.on('window-all-closed', async () => {
if(process.platform !== 'darwin')
{
await this.teardown();
}
});
}
async teardown()
{
....
this.app.quit();
this.app.exit(0);
}
}
simplified unit test example
it('open/close window w/o quitting Electron', async () => {
// (1) arrange
const my_app = new My_Electron_App();
const stub_1 = sinon.stub(my_app, 'teardown').callsFake(async () => {
});
// (2) act
// code that opens a new window on `my_app` instance
...
// code that then closes that particular window instance on `my_app`
...
// (3) assert
...
// clean up
stub_1.restore();
});
If there is another window open, the closing of that new window works just fine. But if the closed window was the last open window, then Electron
quits (and e-mocha
tests stop) although we did stub the my_app.teardown()
method... And as mentioned, stubbing the Electron
app obj itself directly also does not work consistently.
Thanks !
I'd suggest to add breakpoints or debug messages to your event handlers and to the teardown method in particular, to make sure it's not called.
Hello,
running into an issue here and cannot find a solution. I did search and found similar threads from the past (e.g. #67), but no clear solution that works in our scenario.
Problem: Our
e-mocha
tests unexpectedly quit yday although no error occurred. This only occurs sometimes, but obviously breaks the CI. The test block where it (sometimes) breaks opens a few browser windows. At this point they were not closed by our code at the end of each test, which we then implemented.Which brings us to the issue that closing a browser window, if it is the only open window, will ultimately quit
Electron
, which then quits/ends the currently runninge-mocha
tests.Tried to
sinon.stub
app.quit()
andapp.exit()
as well asapp.on())
, but that also only works sometimes, not consistently. I know this sounds strange, but for whatever reasonsinon
does not stub these methods consistently - never had this issue before.How are you guys closing the browser windows w/o quitting Electron ?
Thanks !
Specs: Win 10 x64 e-mocha 11.0.2 mocha 9.2.0 electron 12.0.5