Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 3ed46e4

Browse files
authored
Fix unnecessary mutation of options (#590)
* Fix unnecessary mutation of options close #589 * Fix getBrowserOptions mutation * Fix getBrowserOptions tests * Fix lint * Fix test
1 parent bec9fcf commit 3ed46e4

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/utils.test.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,28 +205,38 @@ describe('getBrowserType', () => {
205205
})
206206

207207
describe('getBrowserOptions', () => {
208-
it('should return undefined for empty options', async () => {
208+
it('should return empty object for empty options', () => {
209209
const options = getBrowserOptions(CHROMIUM)
210-
expect(options).toBe(undefined)
210+
expect(options).toStrictEqual({})
211211
})
212212

213-
it('should return root options', async () => {
213+
it('should return root options', () => {
214214
const launchOptions = { headless: false }
215215
const options = getBrowserOptions(CHROMIUM, launchOptions)
216-
expect(options).toBe(launchOptions)
216+
expect(options).toStrictEqual(launchOptions)
217217
})
218218

219-
it('should return options for defined browser', async () => {
219+
it('should return options for defined browser', () => {
220220
const launchOptions = { headless: false, chromium: { headless: true } }
221221
const options = getBrowserOptions(CHROMIUM, launchOptions)
222222
expect(options).toStrictEqual({ headless: true })
223223
})
224224

225-
it('should return root options for other browser', async () => {
225+
it('should return root options for other browser', () => {
226226
const launchOptions = { headless: false, chromium: { headless: true } }
227227
const options = getBrowserOptions(FIREFOX, launchOptions)
228228
expect(options).toStrictEqual({ headless: false })
229229
})
230+
231+
it('should not mutate original options', () => {
232+
const launchOptions = { headless: false, chromium: { headless: true } }
233+
const options = getBrowserOptions(FIREFOX, launchOptions)
234+
expect(options).toStrictEqual({ headless: false })
235+
expect(launchOptions).toStrictEqual({
236+
headless: false,
237+
chromium: { headless: true },
238+
})
239+
})
230240
})
231241

232242
describe('checkBrowserEnv', () => {

src/utils.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,14 @@ export function getBrowserOptions<T>(
164164
browserName: BrowserType,
165165
options?: Options<T>,
166166
): T {
167-
let result: Options<T> | undefined = options
168-
if (result) {
169-
if (result[browserName]) {
170-
result = deepMerge(result, result[browserName]!)
171-
}
172-
BROWSERS.forEach((browser) => {
173-
delete result![browser as BrowserType]
174-
})
175-
return result
176-
}
177-
return result as T
167+
let result: Options<T> = options ? { ...options } : ({} as T)
168+
if (result[browserName]) {
169+
result = deepMerge(result, result[browserName]!)
170+
}
171+
BROWSERS.forEach((browser) => {
172+
delete result![browser as BrowserType]
173+
})
174+
return result
178175
}
179176

180177
export const getSkipFlag = (

0 commit comments

Comments
 (0)