Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support spawnOpts for geckodriver child process #552

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { GeckodriverParameters } from './types.js'
const log = logger('geckodriver')

export async function start (params: GeckodriverParameters) {
const { cacheDir, customGeckoDriverPath, ...startArgs } = params
const { cacheDir, customGeckoDriverPath, spawnOpts, ...startArgs } = params
let geckoDriverPath = (
customGeckoDriverPath ||
process.env.GECKODRIVER_PATH ||
Expand Down Expand Up @@ -42,7 +42,7 @@ export async function start (params: GeckodriverParameters) {

const args = parseParams(startArgs)
log.info(`Starting Geckodriver at ${geckoDriverPath} with params: ${args.join(' ')}`)
return cp.spawn(geckoDriverPath, args)
return cp.spawn(geckoDriverPath, args, spawnOpts)
}

export const download = downloadDriver
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export type LogLevel = 'fatal' | 'error' | 'warn' | 'info' | 'config' | 'debug' | 'trace'

type SpawnOpt = { [key: string]: string | string[] | SpawnOpt }
christian-bromann marked this conversation as resolved.
Show resolved Hide resolved

export interface GeckodriverParameters {
/**
* List of hostnames to allow. By default the value of --host is allowed, and in addition if that's a well
Expand Down Expand Up @@ -77,6 +79,8 @@ export interface GeckodriverParameters {
* @default process.env.GECKODRIVER_CACHE_DIR || os.tmpdir()
*/
cacheDir?: string

spawnOpts?: SpawnOpt
christian-bromann marked this conversation as resolved.
Show resolved Hide resolved
}

declare global {
Expand Down
41 changes: 41 additions & 0 deletions tests/start-unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import cp from 'node:child_process'
import { vi, test, expect } from 'vitest'
import { type GeckodriverParameters, start } from '../src/index.ts'

test('start', async () => {
vi.mock('../src/install.js', () => {
return {
download: vi.fn().mockResolvedValue('foo')
}
})

vi.mock('../src/utils.js', async (original) => {
const actual: any = await original()
return {
hasAccess: vi.fn().mockResolvedValue(true),
parseParams: actual.parseParams
}
})

vi.mock('node:child_process', () => ({
default: {
spawn: vi.fn(),
}
}))

const args: GeckodriverParameters = {
spawnOpts: {
env: {
MOZ_HEADLESS_WIDTH: '720'
}
}
}

await start(args)
expect(cp.spawn).toHaveBeenCalledWith('foo', ['--host=0.0.0.0', '--websocket-port=0'], {
env: {
MOZ_HEADLESS_WIDTH: '720'
}
})
})