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

Commit 5468ffa

Browse files
authored
Add ability to use function in config (#649)
1 parent 48e8da5 commit 5468ffa

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/utils.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ describe('readConfig', () => {
3333
const config = await readConfig()
3434
expect(config).toMatchObject(DEFAULT_CONFIG)
3535
})
36+
it('should execute function if it was specified in configuration', async () => {
37+
jest.mock(
38+
path.join(__dirname, '..', 'jest-playwright.config.js'),
39+
() => () => ({
40+
browsers: ['webkit'],
41+
}),
42+
{ virtual: true },
43+
)
44+
const config = await readConfig()
45+
expect(config.browsers).toEqual(['webkit'])
46+
})
3647
it('should overwrite with a custom configuration', async () => {
3748
const configObject = {
3849
launchOptions: {
@@ -65,9 +76,7 @@ describe('readConfig', () => {
6576
jest.mock(
6677
path.join(__dirname, '..', 'jest-playwright.config.js'),
6778
() => ({
68-
launchOptions: {
69-
headless: true,
70-
},
79+
...configObject,
7180
browsers: ['webkit'],
7281
}),
7382
{ virtual: true },

src/utils.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,16 @@ export const checkDevice = (
9090
}
9191

9292
export const getDisplayName = (browser: string, device: DeviceType): string => {
93+
const result = `browser: ${browser}`
9394
if (device !== null) {
9495
if (typeof device === 'string') {
95-
return `browser: ${browser} device: ${device}`
96+
return `${result} device: ${device}`
9697
}
9798
if (device.name) {
98-
return `browser: ${browser} device: ${device.name}`
99+
return `${result} device: ${device.name}`
99100
}
100101
}
101-
return `browser: ${browser}`
102+
return result
102103
}
103104

104105
export const getBrowserType = (browser?: BrowserType): BrowserType => {
@@ -113,7 +114,7 @@ export const generateKey = (
113114
export const getDeviceBrowserType = (
114115
device: ConfigDeviceType,
115116
availableDevices: Playwright['devices'],
116-
): BrowserType | null => {
117+
): Nullable<BrowserType> => {
117118
if (typeof device === 'string') {
118119
return availableDevices[device].defaultBrowserType as BrowserType
119120
}
@@ -192,7 +193,7 @@ export const getSkipFlag = (
192193
}
193194

194195
export const readConfig = async (
195-
rootDir: string = process.cwd(),
196+
rootDir = process.cwd(),
196197
jestEnvConfig?: JestPlaywrightConfig,
197198
): Promise<JestPlaywrightConfig> => {
198199
if (jestEnvConfig) {
@@ -217,6 +218,10 @@ export const readConfig = async (
217218
}
218219

219220
const localConfig = await require(absConfigPath)
221+
if (typeof localConfig === 'function') {
222+
const config = await localConfig()
223+
return deepMerge<JestPlaywrightConfig>(DEFAULT_CONFIG, config)
224+
}
220225
return deepMerge<JestPlaywrightConfig>(DEFAULT_CONFIG, localConfig)
221226
}
222227

0 commit comments

Comments
 (0)