Files
Samuel Attard 8287520b9b test: use vitest's API and expect directly, drop the mocha shim and chai (#54439)
* test: assert with vitest's matchers instead of chai

Every assertion moves from chai's chainable language to vitest's own
matchers, so an assertion is always a call and never a bare property read:

- `to.equal` → `toBe`, `to.deep.equal`/`eql` → `toEqual`, `to.be.true` →
  `toBe(true)`, `to.have.property` → `toHaveProperty`, `to.include` →
  `toContain`/`toMatchObject`/`toContainEqual`, `to.be.a('string')` →
  `toBeTypeOf('string')`, `instanceOf` → `toBeInstanceOf`, `lengthOf` →
  `toHaveLength`, `above`/`least`/`below`/`most` → `toBeGreaterThan` and
  friends, `match` → `toMatch`, `throw` → `toThrow`, `oneOf` → `toContain`
  with the operands swapped, `closeTo(e, d)` → `Math.abs(x - e) <= d`,
  `members`/`keys` → `arrayContaining`/`Object.keys`. A chain that kept
  going after `.property()` or `.a()` becomes one matcher where vitest has
  one (`toHaveProperty(key, expect.any(String))`) and separate assertions
  otherwise. chai-as-promised becomes `.resolves`/`.rejects`, a failed
  manual check is `assert.fail()` rather than `new chai.AssertionError`.
- vitest reports promise assertions that are not awaited; the app badge,
  webview zoom and `import('electron')` specs had some that never ran and
  now do (the service worker badge ones waited for a count the worker had
  already cleared, so those just check the worker's answer; the zoom fixture
  issued loadURL() from dom-ready, which aborts, and now waits for the load).
- Code that runs outside the spec module graph (itremote bodies in a
  renderer, the utility-process net harness, two crash-case apps) is
  CommonJS and vitest refuses `require('vitest')`, so it loads the same
  module through spec/lib/vitest-cjs.cjs.
- A lint rule (script/lint-plugins/no-chai-assertions.mjs) rejects chai
  chains on `expect()`, and no-unused-expressions is on for the specs, so
  neither form of property-style assertion can come back.
- chai, chai-as-promised, dirty-chai and their @types leave
  spec/package.json; the refresh-page fixture served chai.js only as "a
  large script" and serves the jquery fixture instead.

* test: return promises instead of taking mocha's `done` callback

vitest has no `done` parameter. The tests and hooks that used one now
return a promise instead; where the callback was only ever called as
`done()` it is the promise's `resolve`, and where it was also handed to an
error-first API or called as `done(error)` it keeps mocha's meaning
(reject on a truthy argument, resolve otherwise) through a local `done`.

* test: move the spec runner's own semantics out of the mocha shim

Three things the shim did are not mocha compatibility but how the Electron
specs need to run, whichever API they are written against: every hook and
test starts from a fresh macrotask so that Chromium's message loop turns
between them, `defer()`-ed cleanup runs before the enclosing suites' own
afterEach hooks, and spec/disabled-tests.json can switch a test off by its
title. Those now live in a small custom vitest runner
(spec/vitest/runner.ts) that applies to every test, and the shim only
translates mocha's API.

* test: write the specs against vitest's API instead of mocha's

Mechanical conversion of every spec file and helper:

- `describe`/`it`/`beforeEach`/... are imported from 'vitest' rather than
  taken from the runner's globals; `before`/`after` become
  `beforeAll`/`afterAll`.
- `this.timeout(n)` becomes the `{ timeout: n }` option of the test or
  suite (a hook's second argument; hooks inside a suite that had a timeout
  get it explicitly, since vitest's suite option only reaches the tests), `this.retries(n)` `{ retry: n }`, and
  `this.skip()` the test context's `ctx.skip()`. The two `before` hooks that
  skipped a whole suite now compute the condition up front
  (`describe.runIf`) or per test. `function () {}` callbacks that only
  existed for `this` are arrows again.
- `ifit`/`ifdescribe` map to `it.skip`/`describe.skip`, mocha's chained
  `it(...).timeout(n)` and `this.bail()` are gone, and the two helpers that
  read `this.test` use the context's task state.

* test: remove the mocha compatibility layer

Nothing imports mocha's API any more, so spec/vitest/mocha-compat.ts and the
setup file that installed its globals go, together with @types/mocha and the
Mocha type overloads in spec/ambient.d.ts. Timeouts are vitest's own again
(30 s per test and hook, ELECTRON_SPEC_TIMEOUT raises it for the sanitizer
jobs, replacing MOCHA_TIMEOUT), the junit path variable becomes
ELECTRON_SPEC_JUNIT_FILE, and `closeAllWindows` loses the parameter that a
hook's context argument would otherwise land in.
2026-10-14 10:58:22 -04:00

231 lines
8.3 KiB
TypeScript

import { describe, it } from 'vitest';
import * as cp from 'node:child_process';
import * as path from 'node:path';
import { ifdescribe } from './lib/spec-helpers.ts';
ifdescribe(process.platform === 'darwin' && process.mas)('Mac App Store build', () => {
describe('private API usage', () => {
// Get paths to all Electron binaries
const getElectronBinaries = () => {
const contentsPath = path.dirname(path.dirname(process.execPath));
return {
mainProcess: process.execPath,
framework: path.join(contentsPath, 'Frameworks', 'Electron Framework.framework', 'Electron Framework'),
helpers: {
main: path.join(contentsPath, 'Frameworks', 'Electron Helper.app', 'Contents', 'MacOS', 'Electron Helper'),
gpu: path.join(
contentsPath,
'Frameworks',
'Electron Helper (GPU).app',
'Contents',
'MacOS',
'Electron Helper (GPU)'
),
plugin: path.join(
contentsPath,
'Frameworks',
'Electron Helper (Plugin).app',
'Contents',
'MacOS',
'Electron Helper (Plugin)'
),
renderer: path.join(
contentsPath,
'Frameworks',
'Electron Helper (Renderer).app',
'Contents',
'MacOS',
'Electron Helper (Renderer)'
)
}
};
};
const checkBinaryForPrivateAPIs = (binaryPath: string, binaryName: string) => {
// Use nm without -U to get all symbols (both defined and undefined)
const nmResult = cp.spawnSync('nm', ['-g', binaryPath], {
encoding: 'utf-8',
maxBuffer: 50 * 1024 * 1024
});
if (nmResult.error) {
throw new Error(`Failed to run nm on ${binaryName}: ${nmResult.error.message}`);
}
const symbols = nmResult.stdout;
// List of known private APIs that should not be present in MAS builds
// These are from the mas_avoid_private_macos_api_usage.patch
const privateAPIs = [
'abort_report_np',
'pthread_fchdir_np',
'pthread_chdir_np',
'SetApplicationIsDaemon',
'_LSSetApplicationLaunchServicesServerConnectionStatus',
'CGSSetWindowCaptureExcludeShape',
'CGRegionCreateWithRect',
'CTFontCopyVariationAxesInternal',
'AudioDeviceDuck',
'CGSMainConnectionID',
'IOBluetoothPreferenceSetControllerPowerState',
'CGSSetDenyWindowServerConnections',
'CGFontRenderingGetFontSmoothingDisabled',
'CTFontDescriptorIsSystemUIFont',
'sandbox_init_with_parameters',
'sandbox_create_params',
'sandbox_set_param',
'sandbox_free_params',
'sandbox_compile_string',
'sandbox_apply',
'sandbox_free_profile',
'sandbox_extension_issue_file',
'sandbox_extension_consume',
'sandbox_extension_release',
'_CSCheckFixDisable',
'responsibility_get_pid_responsible_for_pid',
'_NSAppendToKillRing',
'_NSPrependToKillRing',
'_NSYankFromKillRing',
'__NSNewKillRingSequence',
'__NSInitializeKillRing',
'_NSSetKillRingToYankedState'
];
const foundPrivateAPIs: string[] = [];
for (const api of privateAPIs) {
// Check if the symbol appears in the nm output
// Look for ' U ' (undefined) or ' T ' (defined in text section) followed by the API
// nm output format is like: " U _symbol_name"
const regex = new RegExp(`\\s+[UT]\\s+(_${api})\\b`);
const match = symbols.match(regex);
if (match) {
// Keep the full symbol name including the underscore
foundPrivateAPIs.push(match[1]);
}
}
return foundPrivateAPIs;
};
it('should not use private macOS APIs in main process', { timeout: 60000 }, () => {
const binaries = getElectronBinaries();
const foundPrivateAPIs = checkBinaryForPrivateAPIs(binaries.mainProcess, 'Electron main process');
if (foundPrivateAPIs.length > 0) {
throw new Error(
`Found private macOS APIs in main process:\n${foundPrivateAPIs.join('\n')}\n\n` +
'These APIs are not allowed in Mac App Store builds and will cause rejection.'
);
}
// Also check for private framework linkage
const otoolResult = cp.spawnSync('otool', ['-L', binaries.mainProcess], {
encoding: 'utf-8'
});
if (otoolResult.error) {
throw new Error(`Failed to run otool: ${otoolResult.error.message}`);
}
const frameworks = otoolResult.stdout;
if (frameworks.includes('PrivateFrameworks')) {
throw new Error(
'Found linkage to PrivateFrameworks which is not allowed in Mac App Store builds:\n' + frameworks
);
}
});
it('should not use private macOS APIs in Electron Framework', { timeout: 60000 }, () => {
// Check the Electron Framework binary (mentioned in issue #49616)
const binaries = getElectronBinaries();
const foundAPIs = checkBinaryForPrivateAPIs(binaries.framework, 'Electron Framework');
if (foundAPIs.length > 0) {
throw new Error(
`Found private macOS APIs in Electron Framework:\n${foundAPIs.join('\n')}\n\n` +
'These APIs are not allowed in Mac App Store builds and will cause rejection.\n' +
'See patches/chromium/mas_avoid_private_macos_api_usage.patch.patch'
);
}
});
it('should not use private macOS APIs in helper processes', { timeout: 60000 }, () => {
const binaries = getElectronBinaries();
const allFoundAPIs: Record<string, string[]> = {};
for (const [helperName, helperPath] of Object.entries(binaries.helpers)) {
const displayName = `Electron Helper${helperName !== 'main' ? ` (${helperName.charAt(0).toUpperCase() + helperName.slice(1)})` : ''}`;
const foundAPIs = checkBinaryForPrivateAPIs(helperPath, displayName);
if (foundAPIs.length > 0) {
allFoundAPIs[displayName] = foundAPIs;
}
}
if (Object.keys(allFoundAPIs).length > 0) {
const errorLines = Object.entries(allFoundAPIs).map(([helper, apis]) => `${helper}:\n ${apis.join('\n ')}`);
throw new Error(
`Found private macOS APIs in helper processes:\n\n${errorLines.join('\n\n')}\n\n` +
'These APIs are not allowed in Mac App Store builds and will cause rejection.'
);
}
});
it('should not reference private Objective-C classes', { timeout: 60000 }, () => {
// Check for private Objective-C classes (appear as _OBJC_CLASS_$_ClassName)
const privateClasses = ['NSAccessibilityRemoteUIElement', 'CAContext'];
const binaries = getElectronBinaries();
const binariesToCheck = [
{ path: binaries.mainProcess, name: 'Electron main process' },
{ path: binaries.framework, name: 'Electron Framework' },
{ path: binaries.helpers.main, name: 'Electron Helper' },
{ path: binaries.helpers.renderer, name: 'Electron Helper (Renderer)' }
];
const foundClasses: Record<string, string[]> = {};
for (const binary of binariesToCheck) {
const nmResult = cp.spawnSync('nm', ['-g', binary.path], {
encoding: 'utf-8',
maxBuffer: 50 * 1024 * 1024
});
if (nmResult.error) {
throw new Error(`Failed to run nm on ${binary.name}: ${nmResult.error.message}`);
}
const symbols = nmResult.stdout;
const foundInBinary: string[] = [];
for (const className of privateClasses) {
// Look for _OBJC_CLASS_$_ClassName pattern
if (symbols.includes(`_OBJC_CLASS_$_${className}`)) {
foundInBinary.push(className);
}
}
if (foundInBinary.length > 0) {
foundClasses[binary.name] = foundInBinary;
}
}
if (Object.keys(foundClasses).length > 0) {
const errorLines = Object.entries(foundClasses).map(
([binary, classes]) => `${binary}:\n ${classes.join('\n ')}`
);
throw new Error(
`Found references to private Objective-C classes:\n\n${errorLines.join('\n\n')}\n\n` +
'These are not allowed in Mac App Store builds and will cause rejection.'
);
}
});
});
});