test: convert a few more specs to async/await (#40313)

This commit is contained in:
Milan Burda 2023-11-17 10:44:03 +01:00 committed by GitHub
parent 471449d9f6
commit 67894f1493
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 61 deletions

View file

@ -29,16 +29,12 @@ describe('debugger module', () => {
expect(w.webContents.debugger.isAttached()).to.be.true(); expect(w.webContents.debugger.isAttached()).to.be.true();
}); });
it('fails when protocol version is not supported', done => { it('fails when protocol version is not supported', () => {
try { expect(() => w.webContents.debugger.attach('2.0')).to.throw();
w.webContents.debugger.attach('2.0'); expect(w.webContents.debugger.isAttached()).to.be.false();
} catch {
expect(w.webContents.debugger.isAttached()).to.be.false();
done();
}
}); });
it('attaches when no protocol version is specified', async () => { it('attaches when no protocol version is specified', () => {
w.webContents.debugger.attach(); w.webContents.debugger.attach();
expect(w.webContents.debugger.isAttached()).to.be.true(); expect(w.webContents.debugger.isAttached()).to.be.true();
}); });

View file

@ -11,6 +11,7 @@ import * as dbus from 'dbus-native';
import { ifdescribe, startRemoteControlApp } from './lib/spec-helpers'; import { ifdescribe, startRemoteControlApp } from './lib/spec-helpers';
import { promisify } from 'node:util'; import { promisify } from 'node:util';
import { setTimeout } from 'node:timers/promises'; import { setTimeout } from 'node:timers/promises';
import { once } from 'node:events';
describe('powerMonitor', () => { describe('powerMonitor', () => {
let logindMock: any, dbusMockPowerMonitor: any, getCalls: any, emitSignal: any, reset: any; let logindMock: any, dbusMockPowerMonitor: any, getCalls: any, emitSignal: any, reset: any;
@ -77,17 +78,19 @@ describe('powerMonitor', () => {
}); });
describe('when PrepareForSleep(true) signal is sent by logind', () => { describe('when PrepareForSleep(true) signal is sent by logind', () => {
it('should emit "suspend" event', (done) => { it('should emit "suspend" event', async () => {
dbusMockPowerMonitor.once('suspend', () => done()); const suspend = once(dbusMockPowerMonitor, 'suspend');
emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep', emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
'b', [['b', true]]); 'b', [['b', true]]);
await suspend;
}); });
describe('when PrepareForSleep(false) signal is sent by logind', () => { describe('when PrepareForSleep(false) signal is sent by logind', () => {
it('should emit "resume" event', done => { it('should emit "resume" event', async () => {
dbusMockPowerMonitor.once('resume', () => done()); const resume = once(dbusMockPowerMonitor, 'resume');
emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep', emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
'b', [['b', false]]); 'b', [['b', false]]);
await resume;
}); });
it('should have called Inhibit again', async () => { it('should have called Inhibit again', async () => {

View file

@ -1615,7 +1615,7 @@ describe('webContents module', () => {
response.end(); response.end();
break; break;
default: default:
done('unsupported endpoint'); done(new Error('unsupported endpoint'));
} }
}); });
listen(server).then(({ url }) => { listen(server).then(({ url }) => {

View file

@ -8,7 +8,6 @@ import * as fs from 'node:fs';
import * as url from 'node:url'; import * as url from 'node:url';
import * as ChildProcess from 'node:child_process'; import * as ChildProcess from 'node:child_process';
import { EventEmitter, once } from 'node:events'; import { EventEmitter, once } from 'node:events';
import { promisify } from 'node:util';
import { ifit, ifdescribe, defer, itremote, listen } from './lib/spec-helpers'; import { ifit, ifdescribe, defer, itremote, listen } from './lib/spec-helpers';
import { PipeTransport } from './pipe-transport'; import { PipeTransport } from './pipe-transport';
import * as ws from 'ws'; import * as ws from 'ws';
@ -2514,7 +2513,7 @@ describe('font fallback', () => {
}); });
describe('iframe using HTML fullscreen API while window is OS-fullscreened', () => { describe('iframe using HTML fullscreen API while window is OS-fullscreened', () => {
const fullscreenChildHtml = promisify(fs.readFile)( const fullscreenChildHtml = fs.promises.readFile(
path.join(fixturesPath, 'pages', 'fullscreen-oopif.html') path.join(fixturesPath, 'pages', 'fullscreen-oopif.html')
); );
let w: BrowserWindow; let w: BrowserWindow;

View file

@ -3,7 +3,7 @@ import { app, session, BrowserWindow, ipcMain, WebContents, Extension, Session }
import { closeAllWindows, closeWindow } from './lib/window-helpers'; import { closeAllWindows, closeWindow } from './lib/window-helpers';
import * as http from 'node:http'; import * as http from 'node:http';
import * as path from 'node:path'; import * as path from 'node:path';
import * as fs from 'node:fs'; import * as fs from 'node:fs/promises';
import * as WebSocket from 'ws'; import * as WebSocket from 'ws';
import { emittedNTimes, emittedUntil } from './lib/events-helpers'; import { emittedNTimes, emittedUntil } from './lib/events-helpers';
import { ifit, listen } from './lib/spec-helpers'; import { ifit, listen } from './lib/spec-helpers';
@ -200,7 +200,7 @@ describe('chrome extensions', () => {
it('serializes a loaded extension', async () => { it('serializes a loaded extension', async () => {
const extensionPath = path.join(fixtures, 'extensions', 'red-bg'); const extensionPath = path.join(fixtures, 'extensions', 'red-bg');
const manifest = JSON.parse(fs.readFileSync(path.join(extensionPath, 'manifest.json'), 'utf-8')); const manifest = JSON.parse(await fs.readFile(path.join(extensionPath, 'manifest.json'), 'utf-8'));
const customSession = session.fromPartition(`persist:${uuid.v4()}`); const customSession = session.fromPartition(`persist:${uuid.v4()}`);
const extension = await customSession.loadExtension(extensionPath); const extension = await customSession.loadExtension(extensionPath);
expect(extension.id).to.be.a('string'); expect(extension.id).to.be.a('string');
@ -684,16 +684,15 @@ describe('chrome extensions', () => {
let server: http.Server; let server: http.Server;
let port: number; let port: number;
before(async () => { before(async () => {
server = http.createServer((_, res) => { server = http.createServer(async (_, res) => {
fs.readFile(contentPath, (error, content) => { try {
if (error) { const content = await fs.readFile(contentPath, 'utf-8');
res.writeHead(500); res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`Failed to load ${contentPath} : ${error.code}`); res.end(content, 'utf-8');
} else { } catch (error) {
res.writeHead(200, { 'Content-Type': 'text/html' }); res.writeHead(500);
res.end(content, 'utf-8'); res.end(`Failed to load ${contentPath} : ${(error as NodeJS.ErrnoException).code}`);
} }
});
}); });
({ port, url } = await listen(server)); ({ port, url } = await listen(server));

View file

@ -873,7 +873,7 @@ describe('node feature', () => {
}); });
}; };
process.once('unhandledRejection', () => done('catch block is delayed to next tick')); process.once('unhandledRejection', () => done(new Error('catch block is delayed to next tick')));
setTimeout(() => { setTimeout(() => {
f3().catch(() => done()); f3().catch(() => done());

View file

@ -1,6 +1,6 @@
import { expect } from 'chai'; import { expect } from 'chai';
import * as http from 'node:http'; import * as http from 'node:http';
import * as fs from 'node:fs'; import * as fs from 'node:fs/promises';
import * as path from 'node:path'; import * as path from 'node:path';
import * as url from 'node:url'; import * as url from 'node:url';
@ -27,36 +27,27 @@ describe('security warnings', () => {
before(async () => { before(async () => {
// Create HTTP Server // Create HTTP Server
server = http.createServer((request, response) => { server = http.createServer(async (request, response) => {
const uri = url.parse(request.url!).pathname!; const uri = url.parse(request.url!).pathname!;
let filename = path.join(__dirname, 'fixtures', 'pages', uri); let filename = path.join(__dirname, 'fixtures', 'pages', uri);
fs.stat(filename, (error, stats) => { try {
if (error) { const stats = await fs.stat(filename);
response.writeHead(404, { 'Content-Type': 'text/plain' });
response.end();
return;
}
if (stats.isDirectory()) { if (stats.isDirectory()) {
filename += '/index.html'; filename += '/index.html';
} }
fs.readFile(filename, 'binary', (err, file) => { const file = await fs.readFile(filename, 'binary');
if (err) { const cspHeaders = [
response.writeHead(404, { 'Content-Type': 'text/plain' }); ...(useCsp ? ['script-src \'self\' \'unsafe-inline\''] : [])
response.end(); ];
return; response.writeHead(200, { 'Content-Security-Policy': cspHeaders });
} response.write(file, 'binary');
} catch {
response.writeHead(404, { 'Content-Type': 'text/plain' });
}
const cspHeaders = [ response.end();
...(useCsp ? ['script-src \'self\' \'unsafe-inline\''] : [])
];
response.writeHead(200, { 'Content-Security-Policy': cspHeaders });
response.write(file, 'binary');
response.end();
});
});
}); });
serverUrl = `http://localhost2:${(await listen(server)).port}`; serverUrl = `http://localhost2:${(await listen(server)).port}`;

View file

@ -2,7 +2,7 @@ import { BrowserWindow, Session, session } from 'electron/main';
import { expect } from 'chai'; import { expect } from 'chai';
import * as path from 'node:path'; import * as path from 'node:path';
import * as fs from 'node:fs'; import * as fs from 'node:fs/promises';
import * as http from 'node:http'; import * as http from 'node:http';
import { closeWindow } from './lib/window-helpers'; import { closeWindow } from './lib/window-helpers';
import { ifit, ifdescribe, listen } from './lib/spec-helpers'; import { ifit, ifdescribe, listen } from './lib/spec-helpers';
@ -43,19 +43,18 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', function ()
} }
// Setup a server to download hunspell dictionary. // Setup a server to download hunspell dictionary.
const server = http.createServer((req, res) => { const server = http.createServer(async (req, res) => {
// The provided is minimal dict for testing only, full list of words can // The provided is minimal dict for testing only, full list of words can
// be found at src/third_party/hunspell_dictionaries/xx_XX.dic. // be found at src/third_party/hunspell_dictionaries/xx_XX.dic.
fs.readFile(path.join(__dirname, '/../../third_party/hunspell_dictionaries/xx-XX-3-0.bdic'), function (err, data) { try {
if (err) { const data = await fs.readFile(path.join(__dirname, '/../../third_party/hunspell_dictionaries/xx-XX-3-0.bdic'));
console.error('Failed to read dictionary file');
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200); res.writeHead(200);
res.end(data); res.end(data);
}); } catch (err) {
console.error('Failed to read dictionary file');
res.writeHead(404);
res.end(JSON.stringify(err));
}
}); });
let serverUrl: string; let serverUrl: string;
before(async () => { before(async () => {