From 85643d44e5feb981e221c3e16ce08f260bed3a53 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Sat, 11 Apr 2026 22:59:24 -0700 Subject: [PATCH] chore: migrate api-protocol-spec.ts to vitest --- ...-protocol-spec.ts => api-protocol.spec.ts} | 148 ++++++++++-------- 1 file changed, 82 insertions(+), 66 deletions(-) rename spec/{api-protocol-spec.ts => api-protocol.spec.ts} (96%) diff --git a/spec/api-protocol-spec.ts b/spec/api-protocol.spec.ts similarity index 96% rename from spec/api-protocol-spec.ts rename to spec/api-protocol.spec.ts index 50412bfc75..dfb0e91c09 100644 --- a/spec/api-protocol-spec.ts +++ b/spec/api-protocol.spec.ts @@ -2,6 +2,7 @@ import { protocol, webContents, WebContents, session, BrowserWindow, ipcMain, ne import { expect } from 'chai'; import { v4 } from 'uuid'; +import { afterAll, afterEach, beforeAll, beforeEach, describe, it } from 'vitest'; import * as ChildProcess from 'node:child_process'; import { EventEmitter, once } from 'node:events'; @@ -16,7 +17,7 @@ import { setTimeout } from 'node:timers/promises'; import * as url from 'node:url'; import { collectStreamBody, getResponse } from './lib/net-helpers'; -import { listen, defer, ifit } from './lib/spec-helpers'; +import { listen, defer, ifit, withDone } from './lib/spec-helpers'; import { WebmGenerator } from './lib/video-helpers'; import { closeAllWindows, closeWindow } from './lib/window-helpers'; @@ -83,10 +84,10 @@ function deferPromise(): Promise & { resolve: Function; reject: Function } describe('protocol module', () => { let contents: WebContents; // NB. sandbox: true is used because it makes navigations much (~8x) faster. - before(() => { + beforeAll(() => { contents = (webContents as typeof ElectronInternal.WebContents).create({ sandbox: true }); }); - after(() => contents.destroy()); + afterAll(() => contents.destroy()); async function ajax(url: string, options = {}) { // Note that we need to do navigation every time after a protocol is @@ -356,7 +357,7 @@ describe('protocol module', () => { res.end(text); } }); - after(() => server.close()); + defer(() => server.close()); const { port } = await listen(server); const url = `${protocolName}://fake-host`; const redirectURL = `http://127.0.0.1:${port}/serverRedirect`; @@ -366,17 +367,20 @@ describe('protocol module', () => { expect(r.data).to.equal(text); }); - it('can access request headers', (done) => { - protocol.registerHttpProtocol(protocolName, (request) => { - try { - expect(request).to.have.property('headers'); - done(); - } catch (e) { - done(e); - } - }); - ajax(protocolName + '://fake-host').catch(() => {}); - }); + it( + 'can access request headers', + withDone((done) => { + protocol.registerHttpProtocol(protocolName, (request) => { + try { + expect(request).to.have.property('headers'); + done(); + } catch (e) { + done(e); + } + }); + ajax(protocolName + '://fake-host').catch(() => {}); + }) + ); }); } @@ -644,7 +648,7 @@ describe('protocol module', () => { // FIXME(zcbenz): This test was passing because the test itself was wrong, // I don't know whether it ever passed before and we should take a look at // it in future. - xit('can send POST request', async () => { + it.skip('can send POST request', async () => { const server = http.createServer((req, res) => { let body = ''; req.on('data', (chunk) => { @@ -655,7 +659,7 @@ describe('protocol module', () => { }); server.close(); }); - after(() => server.close()); + defer(() => server.close()); const { url } = await listen(server); interceptHttpProtocol('http', (request, callback) => { const data: Electron.ProtocolResponse = { @@ -679,7 +683,7 @@ describe('protocol module', () => { expect(details.url).to.equal('http://fake-host/'); callback({ cancel: true }); }); - after(() => customSession.webRequest.onBeforeRequest(null)); + defer(() => customSession.webRequest.onBeforeRequest(null)); interceptHttpProtocol('http', (request, callback) => { callback({ @@ -690,17 +694,20 @@ describe('protocol module', () => { await expect(ajax('http://fake-host')).to.be.eventually.rejectedWith(Error); }); - it('can access request headers', (done) => { - protocol.interceptHttpProtocol('http', (request) => { - try { - expect(request).to.have.property('headers'); - done(); - } catch (e) { - done(e); - } - }); - ajax('http://fake-host').catch(() => {}); - }); + it( + 'can access request headers', + withDone((done) => { + protocol.interceptHttpProtocol('http', (request) => { + try { + expect(request).to.have.property('headers'); + done(); + } catch (e) { + done(e); + } + }); + ajax('http://fake-host').catch(() => {}); + }) + ); }); describe('protocol.interceptStreamProtocol', () => { @@ -812,7 +819,7 @@ describe('protocol module', () => { }); } }); - after(() => protocol.unregisterProtocol(serviceWorkerScheme)); + afterAll(() => protocol.unregisterProtocol(serviceWorkerScheme)); it('should fail when registering invalid service worker', async () => { await contents.loadURL(`${serviceWorkerScheme}://${v4()}.com`); @@ -889,21 +896,27 @@ describe('protocol module', () => { await requestReceived; }); - it('can access files through the FileSystem API', (done) => { - const filePath = path.join(fixturesPath, 'pages', 'filesystem.html'); - protocol.registerFileProtocol(standardScheme, (request, callback) => callback({ path: filePath })); - w.loadURL(origin); - ipcMain.once('file-system-error', (event, err) => done(err)); - ipcMain.once('file-system-write-end', () => done()); - }); + it( + 'can access files through the FileSystem API', + withDone((done) => { + const filePath = path.join(fixturesPath, 'pages', 'filesystem.html'); + protocol.registerFileProtocol(standardScheme, (request, callback) => callback({ path: filePath })); + w.loadURL(origin); + ipcMain.once('file-system-error', (event, err) => done(err)); + ipcMain.once('file-system-write-end', () => done()); + }) + ); - it('registers secure, when {secure: true}', (done) => { - const filePath = path.join(fixturesPath, 'pages', 'cache-storage.html'); - ipcMain.once('success', () => done()); - ipcMain.once('failure', (event, err) => done(err)); - protocol.registerFileProtocol(standardScheme, (request, callback) => callback({ path: filePath })); - w.loadURL(origin); - }); + it( + 'registers secure, when {secure: true}', + withDone((done) => { + const filePath = path.join(fixturesPath, 'pages', 'cache-storage.html'); + ipcMain.once('success', () => done()); + ipcMain.once('failure', (event, err) => done(err)); + protocol.registerFileProtocol(standardScheme, (request, callback) => callback({ path: filePath })); + w.loadURL(origin); + }) + ); }); describe('protocol.registerSchemesAsPrivileged cors-fetch', function () { @@ -1016,7 +1029,7 @@ describe('protocol module', () => { const videoPath = path.join(fixturesPath, 'video.webm'); let w: BrowserWindow; - before(async () => { + beforeAll(async () => { // generate test video const imageBase64 = await fs.promises.readFile(videoSourceImagePath, 'base64'); const imageDataUrl = `data:image/webp;base64,${imageBase64}`; @@ -1031,11 +1044,11 @@ describe('protocol module', () => { }); }); - after(async () => { + afterAll(async () => { await fs.promises.unlink(videoPath); }); - beforeEach(async function () { + beforeEach(async (ctx) => { w = new BrowserWindow({ show: false }); await w.loadURL('about:blank'); if ( @@ -1043,7 +1056,7 @@ describe('protocol module', () => { "document.createElement('video').canPlayType('video/webm; codecs=\"vp8.0\"')" )) ) { - this.skip(); + ctx.skip(); } }); @@ -1183,28 +1196,31 @@ describe('protocol module', () => { expect(body).to.equal('hello https://foo/'); }); - it('receives requests to the existing file scheme', (done) => { - const filePath = path.join(__dirname, 'fixtures', 'pages', 'a.html'); + it( + 'receives requests to the existing file scheme', + withDone((done) => { + const filePath = path.join(__dirname, 'fixtures', 'pages', 'a.html'); - protocol.handle('file', (req) => { - let file; - if (process.platform === 'win32') { - file = `file:///${filePath.replaceAll('\\', '/')}`; - } else { - file = `file://${filePath}`; - } + protocol.handle('file', (req) => { + let file; + if (process.platform === 'win32') { + file = `file:///${filePath.replaceAll('\\', '/')}`; + } else { + file = `file://${filePath}`; + } - if (req.url === file) done(); - return new Response(req.url); - }); + if (req.url === file) done(); + return new Response(req.url); + }); - defer(() => { - protocol.unhandle('file'); - }); + defer(() => { + protocol.unhandle('file'); + }); - const w = new BrowserWindow(); - w.loadFile(filePath); - }); + const w = new BrowserWindow(); + w.loadFile(filePath); + }) + ); it('receives requests to an existing scheme when navigating', async () => { protocol.handle('https', (req) => new Response('hello ' + req.url));