Files
electron/spec/_vitest_runner/setup.ts
Samuel Attard 3100799343 refactor: move no-windows-leaked assertion to a global afterAll
closeWindow's assertNotWindows path tried to assert 'this was the only
window' one tick after closing, which only worked if the next-tick fired
after the outer afterEach(closeAllWindows) — a race that mostly held under
mocha and doesn't under vitest.

Replace it with assertNoWindowsLeaked() registered as a setupFiles-level
afterAll — it runs once per file after every test-file afterAll, so suites
that share a window across tests (useRemoteContext, the api-context-bridge
reuse, etc.) aren't flagged on every test. Verified ordering with probe
files: a describe-level afterAll(closeWindow) runs before the global check,
and a file that never cleans up is flagged.

closeWindow is now a plain awaited close; the assertNotWindows option and
its 7 call-site opt-outs are removed. useRemoteContext's afterAll now awaits
'closed' so the shared window is actually gone before the leak check fires.
2026-04-13 01:00:14 -07:00

45 lines
1.5 KiB
TypeScript

import * as chai from 'chai';
import { afterAll, beforeEach } from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { runCleanupFunctions } from '../lib/defer-helpers';
import { assertNoWindowsLeaked } from '../lib/window-helpers';
import chaiAsPromised = require('chai-as-promised');
import dirtyChai = require('dirty-chai');
chai.use(chaiAsPromised);
chai.use(dirtyChai);
// Show full object diff.
// https://github.com/chaijs/chai/issues/469
chai.config.truncateThreshold = 0;
// Skip any tests listed in disabled-tests.json.
const disabledTests = new Set(JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'disabled-tests.json'), 'utf8')));
beforeEach((ctx) => {
// Fallback drain for defer()ed cleanups. Most tests use afterEach(closeAllWindows)
// which already runs these first; this catches tests that defer() without it.
// Note: onTestFinished runs *after* afterEach, so callbacks reaching here must
// not assume windows still exist.
ctx.onTestFinished(runCleanupFunctions);
const parts: string[] = [ctx.task.name];
let suite = ctx.task.suite;
while (suite) {
if (suite.name) parts.unshift(suite.name);
suite = suite.suite;
}
if (disabledTests.has(parts.join(' '))) {
ctx.skip();
}
});
// Runs once per file, after all test-file-level afterAll hooks (setupFiles
// hooks are outermost). Using afterAll rather than afterEach so suites that
// intentionally share a window across tests (useRemoteContext, etc.) are not
// flagged on every test.
afterAll(assertNoWindowsLeaked);