diff --git a/spec/api-web-contents-spec.ts b/spec/api-web-contents.spec.ts similarity index 92% rename from spec/api-web-contents-spec.ts rename to spec/api-web-contents.spec.ts index ff18699e24..76ddbdfebe 100644 --- a/spec/api-web-contents-spec.ts +++ b/spec/api-web-contents.spec.ts @@ -11,6 +11,7 @@ import { } from 'electron/main'; import { assert, expect } from 'chai'; +import { afterAll, afterEach, beforeAll, beforeEach, describe, it } from 'vitest'; import * as cp from 'node:child_process'; import { once } from 'node:events'; @@ -22,7 +23,7 @@ import * as path from 'node:path'; import { setTimeout } from 'node:timers/promises'; import * as url from 'node:url'; -import { ifdescribe, defer, waitUntil, listen, ifit } from './lib/spec-helpers'; +import { ifdescribe, defer, waitUntil, listen, ifit, withDone } from './lib/spec-helpers'; import { cleanupWebContents, closeAllWindows } from './lib/window-helpers'; const fixturesPath = path.resolve(__dirname, 'fixtures'); @@ -217,25 +218,28 @@ describe('webContents module', () => { }).to.throw('Missing required channel argument'); }); - it('does not block node async APIs when sent before document is ready', (done) => { - // Please reference https://github.com/electron/electron/issues/19368 if - // this test fails. - ipcMain.once('async-node-api-done', () => { - done(); - }); - const w = new BrowserWindow({ - show: false, - webPreferences: { - nodeIntegration: true, - sandbox: false, - contextIsolation: false - } - }); - w.loadFile(path.join(fixturesPath, 'pages', 'send-after-node.html')); - setTimeout(50).then(() => { - w.webContents.send('test'); - }); - }); + it( + 'does not block node async APIs when sent before document is ready', + withDone((done) => { + // Please reference https://github.com/electron/electron/issues/19368 if + // this test fails. + ipcMain.once('async-node-api-done', () => { + done(); + }); + const w = new BrowserWindow({ + show: false, + webPreferences: { + nodeIntegration: true, + sandbox: false, + contextIsolation: false + } + }); + w.loadFile(path.join(fixturesPath, 'pages', 'send-after-node.html')); + setTimeout(50).then(() => { + w.webContents.send('test'); + }); + }) + ); }); ifdescribe(features.isPrintingEnabled())('webContents.print()', () => { @@ -296,13 +300,16 @@ describe('webContents module', () => { }).to.throw('webContents.print(): Invalid optional callback provided.'); }); - it('fails when an invalid deviceName is passed', (done) => { - w.webContents.print({ deviceName: 'i-am-a-nonexistent-printer' }, (success, reason) => { - expect(success).to.equal(false); - expect(reason).to.match(/Invalid deviceName provided/); - done(); - }); - }); + it( + 'fails when an invalid deviceName is passed', + withDone((done) => { + w.webContents.print({ deviceName: 'i-am-a-nonexistent-printer' }, (success, reason) => { + expect(success).to.equal(false); + expect(reason).to.match(/Invalid deviceName provided/); + done(); + }); + }) + ); it('throws when an invalid pageSize is passed', () => { expect(() => { @@ -386,14 +393,14 @@ describe('webContents module', () => { let server: http.Server; let serverUrl: string; - before(async () => { + beforeAll(async () => { server = http.createServer((request, response) => { response.end(); }); serverUrl = (await listen(server)).url; }); - after(() => { + afterAll(() => { server.close(); }); @@ -421,12 +428,12 @@ describe('webContents module', () => { describe('webContents.executeJavaScriptInIsolatedWorld', () => { let w: BrowserWindow; - before(async () => { + beforeAll(async () => { w = new BrowserWindow({ show: false, webPreferences: { contextIsolation: true } }); await w.loadURL('about:blank'); }); - after(() => w.close()); + afterAll(() => w.close()); it('resolves the returned promise with the result', async () => { await w.webContents.executeJavaScriptInIsolatedWorld(999, [{ code: 'window.X = 123' }]); @@ -441,7 +448,7 @@ describe('webContents module', () => { let w: BrowserWindow; let s: http.Server; - before(function () { + beforeAll(function () { session .fromPartition('loadurl-webcontents-spec') .setPermissionRequestHandler((webContents, permission, callback) => { @@ -469,7 +476,7 @@ describe('webContents module', () => { }); afterEach(closeAllWindows); - after(async () => { + afterAll(async () => { session.fromPartition('loadurl-webcontents-spec').setPermissionRequestHandler(null); }); @@ -544,18 +551,21 @@ describe('webContents module', () => { } }); - it('fails if loadURL is called inside did-start-loading', (done) => { - w.webContents.once('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => { - expect(validatedURL).to.contain('blank.html'); - done(); - }); + it( + 'fails if loadURL is called inside did-start-loading', + withDone((done) => { + w.webContents.once('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => { + expect(validatedURL).to.contain('blank.html'); + done(); + }); - w.webContents.once('did-start-loading', () => { - w.loadURL(`file://${fixturesPath}/pages/blank.html`); - }); + w.webContents.once('did-start-loading', () => { + w.loadURL(`file://${fixturesPath}/pages/blank.html`); + }); - w.loadURL('data:text/html,

HELLO

'); - }); + w.loadURL('data:text/html,

HELLO

'); + }) + ); it('fails if loadurl is called after the navigation is ready to commit', () => { w.webContents.once('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => { @@ -570,37 +580,40 @@ describe('webContents module', () => { w.loadURL('data:text/html,

HELLO

'); }); - it('fails if loadURL is called inside did-redirect-navigation', (done) => { - const server = http.createServer((req, res) => { - if (req.url === '/302') { - res.statusCode = 302; - res.setHeader('Location', '/200'); - res.end(); - } else if (req.url === '/200') { - res.end('ok'); - } else { - res.end(); - } - }); - - w.webContents.once('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => { - expect(validatedURL).to.contain('blank.html'); - server.close(); - done(); - }); - - listen(server) - .then(({ url }) => { - w.webContents.once('did-redirect-navigation', () => { - w.loadURL(`file://${fixturesPath}/pages/blank.html`); - }); - w.loadURL(`${url}/302`); - }) - .catch((e) => { - server.close(); - done(e); + it( + 'fails if loadURL is called inside did-redirect-navigation', + withDone((done) => { + const server = http.createServer((req, res) => { + if (req.url === '/302') { + res.statusCode = 302; + res.setHeader('Location', '/200'); + res.end(); + } else if (req.url === '/200') { + res.end('ok'); + } else { + res.end(); + } }); - }); + + w.webContents.once('did-fail-load', (_event, _errorCode, _errorDescription, validatedURL) => { + expect(validatedURL).to.contain('blank.html'); + server.close(); + done(); + }); + + listen(server) + .then(({ url }) => { + w.webContents.once('did-redirect-navigation', () => { + w.loadURL(`file://${fixturesPath}/pages/blank.html`); + }); + w.loadURL(`${url}/302`); + }) + .catch((e) => { + server.close(); + done(e); + }); + }) + ); it('sets appropriate error information on rejection', async () => { let err: any; @@ -985,7 +998,7 @@ describe('webContents module', () => { let server: http.Server; let serverUrl: string; - before(async () => { + beforeAll(async () => { server = http.createServer((req, res) => { res.setHeader('Content-Type', 'text/html'); res.end( @@ -995,7 +1008,7 @@ describe('webContents module', () => { serverUrl = (await listen(server)).url; }); - after(async () => { + afterAll(async () => { if (server) await new Promise((resolve) => server.close(resolve)); server = null as any; }); @@ -1882,7 +1895,7 @@ describe('webContents module', () => { host3: 0.2 }; - before(() => { + beforeAll(() => { const protocol = session.defaultSession.protocol; protocol.registerStringProtocol(standardScheme, (request, callback) => { const response = `