fix: abort more descriptively for beforeunload (#49011)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2025-11-19 17:00:51 -05:00 committed by GitHub
commit aeb5af803f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 46 additions and 3 deletions

View file

@ -109,6 +109,7 @@ describe('webContents module', () => {
await closeAllWindows();
await cleanupWebContents();
});
it('does not emit if beforeunload returns undefined in a BrowserWindow', async () => {
const w = new BrowserWindow({ show: false });
w.webContents.once('will-prevent-unload', () => {
@ -162,6 +163,35 @@ describe('webContents module', () => {
w.close();
await wait;
});
it('fails loading a subsequent page after beforeunload is not prevented', async () => {
const w = new BrowserWindow({ show: false });
const didFailLoad = once(w.webContents, 'did-fail-load');
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
await w.webContents.executeJavaScript('console.log(\'gesture\')', true);
w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'a.html'));
const [, code, , validatedURL] = await didFailLoad;
expect(code).to.equal(-3); // ERR_ABORTED
const { href: expectedURL } = url.pathToFileURL(path.join(__dirname, 'fixtures', 'pages', 'a.html'));
expect(validatedURL).to.equal(expectedURL);
});
it('allows loading a subsequent page after beforeunload is prevented', async () => {
const w = new BrowserWindow({ show: false });
w.webContents.once('will-prevent-unload', event => event.preventDefault());
await w.loadFile(path.join(__dirname, 'fixtures', 'api', 'beforeunload-false.html'));
await w.webContents.executeJavaScript('console.log(\'gesture\')', true);
await w.loadFile(path.join(__dirname, 'fixtures', 'pages', 'a.html'));
const pageTitle = await w.webContents.executeJavaScript('document.title');
expect(pageTitle).to.equal('test');
const wait = once(w, 'closed');
w.close();
await wait;
});
});
describe('webContents.send(channel, args...)', () => {