diff --git a/src/utils.test.ts b/src/utils.test.ts index a7f39852..b6a9d529 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -205,28 +205,38 @@ describe('getBrowserType', () => { }) describe('getBrowserOptions', () => { - it('should return undefined for empty options', async () => { + it('should return empty object for empty options', () => { const options = getBrowserOptions(CHROMIUM) - expect(options).toBe(undefined) + expect(options).toStrictEqual({}) }) - it('should return root options', async () => { + it('should return root options', () => { const launchOptions = { headless: false } const options = getBrowserOptions(CHROMIUM, launchOptions) - expect(options).toBe(launchOptions) + expect(options).toStrictEqual(launchOptions) }) - it('should return options for defined browser', async () => { + it('should return options for defined browser', () => { const launchOptions = { headless: false, chromium: { headless: true } } const options = getBrowserOptions(CHROMIUM, launchOptions) expect(options).toStrictEqual({ headless: true }) }) - it('should return root options for other browser', async () => { + it('should return root options for other browser', () => { const launchOptions = { headless: false, chromium: { headless: true } } const options = getBrowserOptions(FIREFOX, launchOptions) expect(options).toStrictEqual({ headless: false }) }) + + it('should not mutate original options', () => { + const launchOptions = { headless: false, chromium: { headless: true } } + const options = getBrowserOptions(FIREFOX, launchOptions) + expect(options).toStrictEqual({ headless: false }) + expect(launchOptions).toStrictEqual({ + headless: false, + chromium: { headless: true }, + }) + }) }) describe('checkBrowserEnv', () => { diff --git a/src/utils.ts b/src/utils.ts index 58af549e..7d68f21a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -164,17 +164,14 @@ export function getBrowserOptions( browserName: BrowserType, options?: Options, ): T { - let result: Options | undefined = options - if (result) { - if (result[browserName]) { - result = deepMerge(result, result[browserName]!) - } - BROWSERS.forEach((browser) => { - delete result![browser as BrowserType] - }) - return result - } - return result as T + let result: Options = options ? { ...options } : ({} as T) + if (result[browserName]) { + result = deepMerge(result, result[browserName]!) + } + BROWSERS.forEach((browser) => { + delete result![browser as BrowserType] + }) + return result } export const getSkipFlag = (