Skip to content

Commit

Permalink
Using deep merge objects and some changes to debug (#249)
Browse files Browse the repository at this point in the history
* Using deep merge objects and some changes to debug

* Fix import LAUNCH from environment

* Improve test
  • Loading branch information
mmarkelov committed Jul 25, 2020
1 parent 7000ffe commit f5579aa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 50 deletions.
17 changes: 3 additions & 14 deletions extends.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* global jestPlaywright, browserName, deviceName */
const { getSkipFlag } = require('./lib/utils')
// TODO Rewrite with TS?
const { getSkipFlag, deepMerge } = require('./lib/utils')

const DEBUG_OPTIONS = {
launchType: 'LAUNCH',
launchOptions: {
headless: false,
devtools: true,
Expand All @@ -14,18 +14,7 @@ const runDebugTest = (jestTestType, ...args) => {
// TODO Looks wierd - need to be rewritten
let options = DEBUG_OPTIONS
if (isConfigProvided) {
const {
contextOptions,
launchOptions = {},
launchType = DEBUG_OPTIONS.launchType,
} = args[0]
// TODO Add function for deep objects merging
options = {
...DEBUG_OPTIONS,
launchType,
launchOptions: { ...DEBUG_OPTIONS.launchOptions, ...launchOptions },
contextOptions,
}
options = deepMerge(DEBUG_OPTIONS, args[0])
}

jestTestType(args[isConfigProvided ? 1 : 0], async () => {
Expand Down
39 changes: 16 additions & 23 deletions src/PlaywrightEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
import type { Event, State } from 'jest-circus'
import type {
Browser,
Page,
BrowserContext,
BrowserContextOptions,
Page,
} from 'playwright-core'
import type {
JestPlaywrightConfig,
GenericBrowser,
BrowserType,
JestPlaywrightProjectConfig,
ConnectOptions,
GenericBrowser,
JestPlaywrightConfig,
JestPlaywrightProjectConfig,
} from '../types/global'
import {
CHROMIUM,
IMPORT_KIND_PLAYWRIGHT,
LAUNCH,
PERSISTENT,
LAUNCH,
} from './constants'
import {
deepMerge,
getBrowserOptions,
getBrowserType,
getDeviceType,
Expand Down Expand Up @@ -174,41 +175,33 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
config: JestPlaywrightConfig,
isDebug?: boolean,
): Promise<ConfigParams> => {
const { contextOptions, launchOptions, launchType } = config
let resultBrowserConfig: JestPlaywrightConfig
let resultContextOptions: BrowserContextOptions | undefined
if (isDebug) {
resultBrowserConfig = config
resultContextOptions = contextOptions
resultContextOptions = config.contextOptions
} else {
// TODO Add function for deep objects merging
resultBrowserConfig = {
...this._jestPlaywrightConfig,
launchType,
launchOptions: {
...this._jestPlaywrightConfig.launchOptions,
...launchOptions,
},
}
resultBrowserConfig = deepMerge(this._jestPlaywrightConfig, {
...config,
launchType: LAUNCH,
})
resultContextOptions = {
...this._jestPlaywrightConfig.contextOptions,
...contextOptions,
...config.contextOptions,
}
}
const browserOrContext = await getBrowserPerProcess(
const browser = await getBrowserPerProcess(
playwrightInstance,
browserType,
resultBrowserConfig,
)
const browser = launchType === PERSISTENT ? null : browserOrContext
const newContextOptions = getBrowserOptions(
browserName,
resultContextOptions,
)
const context =
launchType === PERSISTENT
? (browserOrContext as BrowserContext)
: await (browser as Browser)!.newContext(newContextOptions)
const context = await (browser as Browser)!.newContext(
newContextOptions,
)
const page = await context!.newPage()
return { browser, context, page }
},
Expand Down
4 changes: 4 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ describe('readConfig', () => {
},
browser: 'chromium',
contextOptions: {
viewport: {
width: 800,
height: 640,
},
ignoreHTTPSErrors: true,
},
}
Expand Down
40 changes: 27 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ export const checkBrowserEnv = (param: BrowserType): void => {
}
}

const isObject = (item: any) => {
return item && typeof item === 'object' && !Array.isArray(item)
}

export const deepMerge = <T extends Record<string, any>>(
target: T,
source: T,
): T => {
let output = { ...target }
const keys: (keyof T)[] = Object.keys(source)
if (isObject(target) && isObject(source)) {
keys.forEach((key) => {
if (isObject(source[key])) {
if (!(key in target)) {
output = { ...output, [key]: source[key] }
} else {
output[key] = deepMerge(target[key], source[key])
}
} else {
output = { ...output, [key]: source[key] }
}
})
}
return output
}

export const checkDeviceEnv = (
device: string,
availableDevices: string[],
Expand Down Expand Up @@ -202,19 +228,7 @@ export const readConfig = async (

const localConfig = await require(absConfigPath)
validateConfig(localConfig)
// TODO Add function for deep objects merging
return {
...DEFAULT_CONFIG,
...localConfig,
launchOptions: {
...DEFAULT_CONFIG.launchOptions,
...(localConfig.launchOptions || {}),
},
contextOptions: {
...DEFAULT_CONFIG.contextOptions,
...(localConfig.contextOptions || {}),
},
}
return deepMerge<JestPlaywrightConfig>(DEFAULT_CONFIG, localConfig)
}

export const formatError = (error: string): string =>
Expand Down

0 comments on commit f5579aa

Please sign in to comment.