Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing browser window quits e-mocha tests #194

Open
petef19 opened this issue Feb 10, 2022 · 3 comments
Open

Closing browser window quits e-mocha tests #194

petef19 opened this issue Feb 10, 2022 · 3 comments

Comments

@petef19
Copy link

petef19 commented Feb 10, 2022

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 running e-mocha tests.

Tried to sinon.stub app.quit() and app.exit() as well as app.on()), but that also only works sometimes, not consistently. I know this sounds strange, but for whatever reason sinon 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

@inukshuk
Copy link
Collaborator

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.

@petef19
Copy link
Author

petef19 commented Feb 15, 2022

@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 !

@inukshuk
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants