Skip to content

Commit f5579aa

Browse files
authored
Using deep merge objects and some changes to debug (#249)
* Using deep merge objects and some changes to debug * Fix import LAUNCH from environment * Improve test
1 parent 7000ffe commit f5579aa

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

extends.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* global jestPlaywright, browserName, deviceName */
2-
const { getSkipFlag } = require('./lib/utils')
2+
// TODO Rewrite with TS?
3+
const { getSkipFlag, deepMerge } = require('./lib/utils')
34

45
const DEBUG_OPTIONS = {
5-
launchType: 'LAUNCH',
66
launchOptions: {
77
headless: false,
88
devtools: true,
@@ -14,18 +14,7 @@ const runDebugTest = (jestTestType, ...args) => {
1414
// TODO Looks wierd - need to be rewritten
1515
let options = DEBUG_OPTIONS
1616
if (isConfigProvided) {
17-
const {
18-
contextOptions,
19-
launchOptions = {},
20-
launchType = DEBUG_OPTIONS.launchType,
21-
} = args[0]
22-
// TODO Add function for deep objects merging
23-
options = {
24-
...DEBUG_OPTIONS,
25-
launchType,
26-
launchOptions: { ...DEBUG_OPTIONS.launchOptions, ...launchOptions },
27-
contextOptions,
28-
}
17+
options = deepMerge(DEBUG_OPTIONS, args[0])
2918
}
3019

3120
jestTestType(args[isConfigProvided ? 1 : 0], async () => {

src/PlaywrightEnvironment.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22
import type { Event, State } from 'jest-circus'
33
import type {
44
Browser,
5-
Page,
65
BrowserContext,
76
BrowserContextOptions,
7+
Page,
88
} from 'playwright-core'
99
import type {
10-
JestPlaywrightConfig,
11-
GenericBrowser,
1210
BrowserType,
13-
JestPlaywrightProjectConfig,
1411
ConnectOptions,
12+
GenericBrowser,
13+
JestPlaywrightConfig,
14+
JestPlaywrightProjectConfig,
1515
} from '../types/global'
1616
import {
1717
CHROMIUM,
1818
IMPORT_KIND_PLAYWRIGHT,
19-
LAUNCH,
2019
PERSISTENT,
20+
LAUNCH,
2121
} from './constants'
2222
import {
23+
deepMerge,
2324
getBrowserOptions,
2425
getBrowserType,
2526
getDeviceType,
@@ -174,41 +175,33 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
174175
config: JestPlaywrightConfig,
175176
isDebug?: boolean,
176177
): Promise<ConfigParams> => {
177-
const { contextOptions, launchOptions, launchType } = config
178178
let resultBrowserConfig: JestPlaywrightConfig
179179
let resultContextOptions: BrowserContextOptions | undefined
180180
if (isDebug) {
181181
resultBrowserConfig = config
182-
resultContextOptions = contextOptions
182+
resultContextOptions = config.contextOptions
183183
} else {
184-
// TODO Add function for deep objects merging
185-
resultBrowserConfig = {
186-
...this._jestPlaywrightConfig,
187-
launchType,
188-
launchOptions: {
189-
...this._jestPlaywrightConfig.launchOptions,
190-
...launchOptions,
191-
},
192-
}
184+
resultBrowserConfig = deepMerge(this._jestPlaywrightConfig, {
185+
...config,
186+
launchType: LAUNCH,
187+
})
193188
resultContextOptions = {
194189
...this._jestPlaywrightConfig.contextOptions,
195-
...contextOptions,
190+
...config.contextOptions,
196191
}
197192
}
198-
const browserOrContext = await getBrowserPerProcess(
193+
const browser = await getBrowserPerProcess(
199194
playwrightInstance,
200195
browserType,
201196
resultBrowserConfig,
202197
)
203-
const browser = launchType === PERSISTENT ? null : browserOrContext
204198
const newContextOptions = getBrowserOptions(
205199
browserName,
206200
resultContextOptions,
207201
)
208-
const context =
209-
launchType === PERSISTENT
210-
? (browserOrContext as BrowserContext)
211-
: await (browser as Browser)!.newContext(newContextOptions)
202+
const context = await (browser as Browser)!.newContext(
203+
newContextOptions,
204+
)
212205
const page = await context!.newPage()
213206
return { browser, context, page }
214207
},

src/utils.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ describe('readConfig', () => {
3737
},
3838
browser: 'chromium',
3939
contextOptions: {
40+
viewport: {
41+
width: 800,
42+
height: 640,
43+
},
4044
ignoreHTTPSErrors: true,
4145
},
4246
}

src/utils.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,32 @@ export const checkBrowserEnv = (param: BrowserType): void => {
3030
}
3131
}
3232

33+
const isObject = (item: any) => {
34+
return item && typeof item === 'object' && !Array.isArray(item)
35+
}
36+
37+
export const deepMerge = <T extends Record<string, any>>(
38+
target: T,
39+
source: T,
40+
): T => {
41+
let output = { ...target }
42+
const keys: (keyof T)[] = Object.keys(source)
43+
if (isObject(target) && isObject(source)) {
44+
keys.forEach((key) => {
45+
if (isObject(source[key])) {
46+
if (!(key in target)) {
47+
output = { ...output, [key]: source[key] }
48+
} else {
49+
output[key] = deepMerge(target[key], source[key])
50+
}
51+
} else {
52+
output = { ...output, [key]: source[key] }
53+
}
54+
})
55+
}
56+
return output
57+
}
58+
3359
export const checkDeviceEnv = (
3460
device: string,
3561
availableDevices: string[],
@@ -202,19 +228,7 @@ export const readConfig = async (
202228

203229
const localConfig = await require(absConfigPath)
204230
validateConfig(localConfig)
205-
// TODO Add function for deep objects merging
206-
return {
207-
...DEFAULT_CONFIG,
208-
...localConfig,
209-
launchOptions: {
210-
...DEFAULT_CONFIG.launchOptions,
211-
...(localConfig.launchOptions || {}),
212-
},
213-
contextOptions: {
214-
...DEFAULT_CONFIG.contextOptions,
215-
...(localConfig.contextOptions || {}),
216-
},
217-
}
231+
return deepMerge<JestPlaywrightConfig>(DEFAULT_CONFIG, localConfig)
218232
}
219233

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

0 commit comments

Comments
 (0)