|
1 | 1 | import { vi } from 'vitest'; |
2 | 2 | import { tmpdir } from 'os'; |
3 | 3 | import { join } from 'path'; |
| 4 | +import * as electronStub from './electron-stub'; |
4 | 5 |
|
5 | | -const userDataPath = join(tmpdir(), 'jlab-test-userdata'); |
6 | 6 | const logFilePath = join(tmpdir(), 'jlab-test.log'); |
7 | 7 |
|
8 | | -vi.mock('electron', () => ({ |
9 | | - app: { |
10 | | - getPath: vi.fn((name: string) => join(userDataPath, name)), |
11 | | - getVersion: vi.fn(() => '4.4.7'), |
12 | | - getName: vi.fn(() => 'JupyterLab'), |
13 | | - isPackaged: false, |
14 | | - whenReady: vi.fn(() => Promise.resolve()) |
15 | | - }, |
16 | | - ipcMain: { |
17 | | - on: vi.fn(), |
18 | | - handle: vi.fn(), |
19 | | - removeHandler: vi.fn(), |
20 | | - removeListener: vi.fn(), |
21 | | - removeAllListeners: vi.fn(), |
22 | | - emit: vi.fn() |
23 | | - }, |
24 | | - dialog: { |
25 | | - showOpenDialog: vi.fn(), |
26 | | - showMessageBox: vi.fn(), |
27 | | - showMessageBoxSync: vi.fn(() => 0) |
28 | | - }, |
29 | | - BrowserWindow: vi.fn().mockImplementation(() => ({ |
30 | | - loadURL: vi.fn(), |
31 | | - webContents: { send: vi.fn(), on: vi.fn() }, |
32 | | - on: vi.fn(), |
33 | | - once: vi.fn(), |
34 | | - show: vi.fn(), |
35 | | - close: vi.fn(), |
36 | | - isDestroyed: vi.fn(() => false) |
37 | | - })), |
38 | | - shell: { openExternal: vi.fn(), openPath: vi.fn() }, |
39 | | - nativeTheme: { shouldUseDarkColors: false }, |
40 | | - screen: { |
41 | | - getPrimaryDisplay: vi.fn(() => ({ |
42 | | - workAreaSize: { width: 1920, height: 1080 } |
43 | | - })) |
44 | | - } |
45 | | -})); |
| 8 | +// `electron` is mocked two ways pointing at the same ./electron-stub instances: |
| 9 | +// - vitest.config `resolve.alias` redirects ESM `import ... from 'electron'`. |
| 10 | +// - vitest leaves CJS `require('electron')` (used by the preload scripts) |
| 11 | +// externalized, so it hits Node's require and would return the binary path |
| 12 | +// string. Pre-seeding require.cache for electron's resolved id makes that |
| 13 | +// require return the stub instead. Both paths share electronStub's vi.fns. |
| 14 | +try { |
| 15 | + const electronId = require.resolve('electron'); |
| 16 | + (require.cache as Record<string, unknown>)[electronId] = { |
| 17 | + id: electronId, |
| 18 | + filename: electronId, |
| 19 | + loaded: true, |
| 20 | + exports: electronStub |
| 21 | + }; |
| 22 | +} catch { |
| 23 | + // electron not resolvable in this environment; ESM alias still applies. |
| 24 | +} |
46 | 25 |
|
47 | 26 | vi.mock('electron-log', () => ({ |
48 | 27 | default: { |
|
0 commit comments