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

Commit 85dc15e

Browse files
authored
Add ability to deepMerge arrays (#683)
* Add ability to deepMerge arrays * Remove test suite restriction * Correct prettier style * Correct prettier format * Correct tests by spreading DEFAULT_CONFIG
1 parent d57d136 commit 85dc15e

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

src/PlaywrightEnvironment.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
139139
...DEFAULT_CONFIG,
140140
launchType: LAUNCH,
141141
}
142-
let resultBrowserConfig: JestPlaywrightConfig = deepMerge(
143-
defaultBrowserConfig,
144-
config,
145-
)
142+
let resultBrowserConfig: JestPlaywrightConfig = {
143+
...defaultBrowserConfig,
144+
...config,
145+
}
146146
if (isDebug) {
147147
if (debugOptions) {
148148
resultBrowserConfig = deepMerge(resultBrowserConfig, debugOptions)

src/utils.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
getBrowserOptions,
1818
getDeviceBrowserType,
1919
generateKey,
20+
deepMerge,
2021
} = Utils
2122

2223
beforeEach(() => {
@@ -408,3 +409,72 @@ describe('getPlaywrightInstance', () => {
408409
)
409410
})
410411
})
412+
413+
describe('deepMerge', () => {
414+
it('should return deeply merged object', () => {
415+
const source = {
416+
viewport: { width: 1440, height: 900 },
417+
isMobile: false,
418+
storageState: {
419+
origins: [
420+
{
421+
origin: 'https://google.com',
422+
localStorage: [
423+
{
424+
name: 'name',
425+
value: 'google',
426+
},
427+
],
428+
},
429+
],
430+
},
431+
}
432+
433+
const target = {
434+
viewport: { width: 1920, height: 900 },
435+
isMobile: false,
436+
hasTouch: true,
437+
storageState: {
438+
origins: [
439+
{
440+
origin: 'https://bing.com',
441+
localStorage: [
442+
{
443+
name: 'name',
444+
value: 'bing',
445+
},
446+
],
447+
},
448+
],
449+
},
450+
}
451+
452+
expect(deepMerge(source, target)).toEqual({
453+
viewport: { width: 1920, height: 900 },
454+
isMobile: false,
455+
hasTouch: true,
456+
storageState: {
457+
origins: [
458+
{
459+
origin: 'https://bing.com',
460+
localStorage: [
461+
{
462+
name: 'name',
463+
value: 'bing',
464+
},
465+
],
466+
},
467+
{
468+
origin: 'https://google.com',
469+
localStorage: [
470+
{
471+
name: 'name',
472+
value: 'google',
473+
},
474+
],
475+
},
476+
],
477+
},
478+
})
479+
})
480+
})

src/utils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export const deepMerge = <T extends Record<string, any>>(
5454
const keys: (keyof T)[] = Object.keys(source)
5555
if (isObject(target) && isObject(source)) {
5656
keys.forEach((key) => {
57-
if (isObject(source[key])) {
57+
if (Array.isArray(source[key]) && Array.isArray(target[key])) {
58+
output = { ...output, [key]: [...source[key], ...target[key]] }
59+
} else if (isObject(source[key])) {
5860
if (!(key in target)) {
5961
output = { ...output, [key]: source[key] }
6062
} else {
@@ -197,7 +199,7 @@ export const readConfig = async (
197199
jestEnvConfig?: JestPlaywrightConfig,
198200
): Promise<JestPlaywrightConfig> => {
199201
if (jestEnvConfig) {
200-
return deepMerge<JestPlaywrightConfig>(DEFAULT_CONFIG, jestEnvConfig)
202+
return { ...DEFAULT_CONFIG, ...jestEnvConfig }
201203
}
202204
const { JEST_PLAYWRIGHT_CONFIG, npm_package_type } = process.env
203205
const fileExtension = npm_package_type === 'module' ? 'cjs' : 'js'
@@ -220,9 +222,9 @@ export const readConfig = async (
220222
const localConfig = await require(absConfigPath)
221223
if (typeof localConfig === 'function') {
222224
const config = await localConfig()
223-
return deepMerge<JestPlaywrightConfig>(DEFAULT_CONFIG, config)
225+
return { ...DEFAULT_CONFIG, ...config }
224226
}
225-
return deepMerge<JestPlaywrightConfig>(DEFAULT_CONFIG, localConfig)
227+
return { ...DEFAULT_CONFIG, ...localConfig }
226228
}
227229

228230
export const formatError = (error: string): string =>

0 commit comments

Comments
 (0)