Skip to content

Commit 849f11b

Browse files
committed
test(electron): enhance launcher tests with package reading mocks
- Added mock implementations for `readPackageUp` and `readPackageUpSync` in `launcher.spec.ts` to improve test coverage and reliability. - Updated the `beforeEach` setup to utilize the new mocks, ensuring consistent behavior during tests. - Adjusted import statements in `pathResolver.spec.ts` to source `NormalizedReadResult` from `@wdio/native-types`, maintaining consistency across test files.
1 parent 2009609 commit 849f11b

2 files changed

Lines changed: 53 additions & 47 deletions

File tree

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

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,32 @@ let options: ElectronServiceOptions;
1616
let getBinaryPath: Mock<() => Promise<BinaryPathResult>>;
1717
let getAppBuildInfo: Mock<() => Promise<AppBuildInfo>>;
1818
let getElectronVersion: Mock<() => Promise<string>>;
19+
let readPackageUp: Mock<
20+
(options?: {
21+
cwd?: string;
22+
}) => Promise<
23+
| { packageJson: { dependencies: Record<string, string>; devDependencies: Record<string, string> }; path: string }
24+
| undefined
25+
>
26+
>;
27+
let readPackageUpSync: Mock<
28+
(options?: {
29+
cwd?: string;
30+
}) =>
31+
| { packageJson: { dependencies: Record<string, string>; devDependencies: Record<string, string> }; path: string }
32+
| undefined
33+
>;
1934

2035
function getFixtureDir(fixtureType: string, fixtureName: string) {
2136
return path.join(process.cwd(), '..', '..', 'fixtures', fixtureType, fixtureName);
2237
}
2338

2439
vi.mock('node:fs/promises', () => {
2540
const mockAccessFn = vi.fn().mockResolvedValue(undefined);
26-
const mockReadFileFn = vi.fn();
2741
return {
2842
access: mockAccessFn,
29-
readFile: mockReadFileFn,
3043
default: {
3144
access: mockAccessFn,
32-
readFile: mockReadFileFn,
3345
},
3446
};
3547
});
@@ -38,23 +50,11 @@ vi.mock('@wdio/native-utils', async () => {
3850
const actual = await vi.importActual('@wdio/native-utils');
3951
return {
4052
...actual,
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-
}),
53+
getBinaryPath: vi.fn(),
54+
getAppBuildInfo: vi.fn(),
55+
getElectronVersion: vi.fn(),
56+
readPackageUp: vi.fn(),
57+
readPackageUpSync: vi.fn(),
5858
createLogger: vi.fn(() => ({
5959
info: vi.fn(),
6060
warn: vi.fn(),
@@ -81,6 +81,21 @@ beforeEach(async () => {
8181
getBinaryPath = nativeUtils.getBinaryPath as Mock<() => Promise<BinaryPathResult>>;
8282
getAppBuildInfo = nativeUtils.getAppBuildInfo as Mock<() => Promise<AppBuildInfo>>;
8383
getElectronVersion = nativeUtils.getElectronVersion as Mock<() => Promise<string>>;
84+
readPackageUp = nativeUtils.readPackageUp as Mock<
85+
(
86+
...args: unknown[]
87+
) => Promise<
88+
| { packageJson: { dependencies: Record<string, string>; devDependencies: Record<string, string> }; path: string }
89+
| undefined
90+
>
91+
>;
92+
readPackageUpSync = nativeUtils.readPackageUpSync as Mock<
93+
(
94+
...args: unknown[]
95+
) =>
96+
| { packageJson: { dependencies: Record<string, string>; devDependencies: Record<string, string> }; path: string }
97+
| undefined
98+
>;
8499
getBinaryPath.mockResolvedValue({
85100
ok: true,
86101
value: {
@@ -116,6 +131,22 @@ beforeEach(async () => {
116131
// Default getElectronVersion mock - returns a version >= 26 by default
117132
getElectronVersion.mockResolvedValue('30.0.0');
118133

134+
// Default readPackageUp mock - returns a valid result based on the cwd option
135+
readPackageUp.mockImplementation(async (options?: { cwd?: string }) => {
136+
const cwdPath = options?.cwd || getFixtureDir('package-scenarios', 'no-build-tool');
137+
return {
138+
packageJson: { dependencies: { electron: '^26.0.0' }, devDependencies: {} },
139+
path: path.join(cwdPath, 'package.json'),
140+
};
141+
});
142+
readPackageUpSync.mockImplementation((options?: { cwd?: string }) => {
143+
const cwdPath = options?.cwd || getFixtureDir('package-scenarios', 'no-build-tool');
144+
return {
145+
packageJson: { dependencies: { electron: '^26.0.0' }, devDependencies: {} },
146+
path: path.join(cwdPath, 'package.json'),
147+
};
148+
});
149+
119150
LaunchService = (await import('../src/launcher.js')).default;
120151
options = {
121152
appBinaryPath: 'workspace/my-test-app/dist/my-test-app',
@@ -836,7 +867,6 @@ describe('Electron Launch Service', () => {
836867
});
837868

838869
it('should use readPackageUp result for electron binary path with appEntryPoint instead of rootDir', async () => {
839-
// Mock readPackageUp in @wdio/native-utils before importing the module
840870
const mockPackage = {
841871
packageJson: {
842872
dependencies: { electron: '^26.0.0' },
@@ -847,31 +877,11 @@ describe('Electron Launch Service', () => {
847877
path: '/different/path/to/package.json',
848878
};
849879

850-
vi.doMock('@wdio/native-utils', () => ({
851-
...vi.importActual('@wdio/native-utils'),
852-
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-
})),
863-
}));
864-
865-
// Clear module cache to ensure our mock is used
866-
vi.resetModules();
867-
868-
// Now import the module after setting up the mock
869-
const { default: LaunchService } = await import('../src/launcher.js');
880+
readPackageUp.mockResolvedValue(mockPackage);
870881

871882
delete options.appBinaryPath;
872883
options.appEntryPoint = 'path/to/main.bundle.js';
873884

874-
// Create instance with a different rootDir to ensure we don't use it
875885
instance = new LaunchService(
876886
options,
877887
[] as never,
@@ -890,7 +900,6 @@ describe('Electron Launch Service', () => {
890900

891901
await instance?.onPrepare({} as never, capabilities);
892902

893-
// Verify that binary path uses the directory from readPackageUp result, not rootDir
894903
expect(capabilities[0]['goog:chromeOptions']?.binary).toBe(
895904
path.join(
896905
'/different/path/to',
@@ -899,9 +908,6 @@ describe('Electron Launch Service', () => {
899908
process.platform === 'win32' ? 'electron.CMD' : 'electron',
900909
),
901910
);
902-
903-
// Note: Not cleaning up the read-package-up mock to avoid Windows path issues
904-
// The mock won't affect other tests since they don't use read-package-up
905911
});
906912

907913
it('should set the expected capabilities when setting custom chromedriverOptions', async () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { access } from 'node:fs/promises';
2-
import type { NormalizedReadResult } from '@wdio/native-utils';
2+
import type { NormalizedReadResult } from '@wdio/native-types';
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
44
import { resolveAppPaths, validateFilePath } from '../src/pathResolver.js';
55

0 commit comments

Comments
 (0)