diff --git a/lib/browser/api/browser-window.ts b/lib/browser/api/browser-window.ts index b6ec4e60b6d5..eb0984409fe5 100644 --- a/lib/browser/api/browser-window.ts +++ b/lib/browser/api/browser-window.ts @@ -54,7 +54,7 @@ BrowserWindow.prototype._init = function (this: BWT) { this._browserViews = []; - this.on('close', () => { + this.on('closed', () => { this._browserViews.forEach(b => b.webContents?.close({ waitForBeforeUnload: true })); }); diff --git a/spec/api-browser-view-spec.ts b/spec/api-browser-view-spec.ts index 24551bfffbd7..45ed4cfa7bf5 100644 --- a/spec/api-browser-view-spec.ts +++ b/spec/api-browser-view-spec.ts @@ -25,10 +25,12 @@ describe('BrowserView module', () => { }); afterEach(async () => { - const p = once(w.webContents, 'destroyed'); - await closeWindow(w); - w = null as any; - await p; + if (!w.isDestroyed()) { + const p = once(w.webContents, 'destroyed'); + await closeWindow(w); + w = null as any; + await p; + } if (view && view.webContents) { const p = once(view.webContents, 'destroyed'); @@ -534,6 +536,53 @@ describe('BrowserView module', () => { }); describe('shutdown behavior', () => { + it('emits the destroyed event when the host BrowserWindow is closed', async () => { + view = new BrowserView(); + w.addBrowserView(view); + await view.webContents.loadURL(`data:text/html, + +
+