Skip to content

Commit c12ad90

Browse files
committed
test(electron): update mocks for read-package-up replacement
- Added mock implementations for `readFile` and updated `readPackageUp` in `@wdio/native-utils` to improve test coverage and reliability. - Adjusted import statements in `api.spec.ts` to use `NormalizedPackageJson` from `@wdio/native-types`, ensuring consistency across test files.
1 parent 0e4dde6 commit c12ad90

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

e2e/test/electron/standalone/api.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
3+
import type { NormalizedPackageJson } from '@wdio/native-types';
34
import { getElectronVersion } from '@wdio/native-utils';
45
import type * as Electron from 'electron';
5-
import type { NormalizedPackageJson } from 'read-package-up';
66
import { setupStandaloneTest } from './helpers/setup.js';
77

88
console.log('🔍 Debug: Starting Electron standalone API test');

packages/electron-service/test/launcher.spec.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ function getFixtureDir(fixtureType: string, fixtureName: string) {
2323

2424
vi.mock('node:fs/promises', () => {
2525
const mockAccessFn = vi.fn().mockResolvedValue(undefined);
26+
const mockReadFileFn = vi.fn();
2627
return {
2728
access: mockAccessFn,
29+
readFile: mockReadFileFn,
2830
default: {
2931
access: mockAccessFn,
32+
readFile: mockReadFileFn,
3033
},
3134
};
3235
});
@@ -35,9 +38,23 @@ vi.mock('@wdio/native-utils', async () => {
3538
const actual = await vi.importActual('@wdio/native-utils');
3639
return {
3740
...actual,
38-
getBinaryPath: vi.fn(),
39-
getAppBuildInfo: vi.fn(),
40-
getElectronVersion: vi.fn(),
41+
getBinaryPath: vi.fn().mockResolvedValue({ ok: true, value: { binaryPath: '' } }),
42+
getAppBuildInfo: vi.fn().mockResolvedValue({ ok: true, value: { appName: '', config: {} } }),
43+
getElectronVersion: vi.fn().mockResolvedValue('26.0.0'),
44+
readPackageUp: vi.fn().mockImplementation((options: { cwd?: string }) => {
45+
const cwd = (options as { cwd?: string }).cwd || process.cwd();
46+
return Promise.resolve({
47+
packageJson: { name: 'test', version: '1.0.0', dependencies: {}, devDependencies: {} },
48+
path: `${cwd}/package.json`,
49+
});
50+
}),
51+
readPackageUpSync: vi.fn().mockImplementation((options: { cwd?: string }) => {
52+
const cwd = (options as { cwd?: string }).cwd || process.cwd();
53+
return {
54+
packageJson: { name: 'test', version: '1.0.0', dependencies: {}, devDependencies: {} },
55+
path: `${cwd}/package.json`,
56+
};
57+
}),
4158
createLogger: vi.fn(() => ({
4259
info: vi.fn(),
4360
warn: vi.fn(),
@@ -819,7 +836,7 @@ describe('Electron Launch Service', () => {
819836
});
820837

821838
it('should use readPackageUp result for electron binary path with appEntryPoint instead of rootDir', async () => {
822-
// Mock readPackageUp before importing the module
839+
// Mock readPackageUp in @wdio/native-utils before importing the module
823840
const mockPackage = {
824841
packageJson: {
825842
dependencies: { electron: '^26.0.0' },
@@ -830,8 +847,19 @@ describe('Electron Launch Service', () => {
830847
path: '/different/path/to/package.json',
831848
};
832849

833-
vi.doMock('read-package-up', () => ({
850+
vi.doMock('@wdio/native-utils', () => ({
851+
...vi.importActual('@wdio/native-utils'),
834852
readPackageUp: vi.fn().mockResolvedValue(mockPackage),
853+
getBinaryPath: vi.fn(),
854+
getAppBuildInfo: vi.fn(),
855+
getElectronVersion: vi.fn(),
856+
createLogger: vi.fn(() => ({
857+
info: vi.fn(),
858+
warn: vi.fn(),
859+
debug: vi.fn(),
860+
error: vi.fn(),
861+
trace: vi.fn(),
862+
})),
835863
}));
836864

837865
// Clear module cache to ensure our mock is used

0 commit comments

Comments
 (0)