Skip to content

Commit 9f4f2e1

Browse files
authored
Add ability to pass config with jest config (#282)
1 parent 0f4489c commit 9f4f2e1

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

src/PlaywrightEnvironment.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import {
1818
IMPORT_KIND_PLAYWRIGHT,
1919
PERSISTENT,
2020
LAUNCH,
21+
CONFIG_ENVIRONMENT_NAME,
2122
} from './constants'
2223
import {
2324
deepMerge,
2425
getBrowserOptions,
2526
getBrowserType,
2627
getDeviceType,
2728
getPlaywrightInstance,
28-
readConfig,
2929
} from './utils'
3030
import { saveCoverageOnPage, saveCoverageToFile } from './coverage'
3131

@@ -77,7 +77,6 @@ const getBrowserPerProcess = async (
7777
}
7878

7979
export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
80-
// eslint-disable-next-line @typescript-eslint/no-var-requires
8180
const RootEnv = require(basicEnv === 'node'
8281
? 'jest-environment-node'
8382
: 'jest-environment-jsdom')
@@ -92,8 +91,9 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
9291
}
9392

9493
async setup(): Promise<void> {
95-
const { rootDir, wsEndpoint, browserName } = this._config
96-
this._jestPlaywrightConfig = await readConfig(rootDir)
94+
const { wsEndpoint, browserName, testEnvironmentOptions } = this._config
95+
this._jestPlaywrightConfig =
96+
testEnvironmentOptions[CONFIG_ENVIRONMENT_NAME]
9797
const {
9898
connectOptions,
9999
collectCoverage,
@@ -102,7 +102,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
102102
launchType,
103103
debugOptions,
104104
} = this._jestPlaywrightConfig
105-
if (wsEndpoint && !connectOptions?.wsEndpoint) {
105+
if (wsEndpoint) {
106106
this._jestPlaywrightConfig.connectOptions = {
107107
...connectOptions,
108108
wsEndpoint,

src/PlaywrightRunner.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,33 @@ import {
2525
getPlaywrightInstance,
2626
getBrowserOptions,
2727
} from './utils'
28-
import { DEFAULT_TEST_PLAYWRIGHT_TIMEOUT, SERVER } from './constants'
28+
import {
29+
DEFAULT_TEST_PLAYWRIGHT_TIMEOUT,
30+
CONFIG_ENVIRONMENT_NAME,
31+
SERVER,
32+
} from './constants'
2933
import { BrowserServer } from 'playwright-core'
3034
import { setupCoverage, mergeCoverage } from './coverage'
3135

3236
const getBrowserTest = (
3337
test: JestPlaywrightTest,
38+
config: JestPlaywrightConfig,
3439
browser: BrowserType,
3540
wsEndpoint: WsEndpointType,
3641
device: DeviceType,
3742
): JestPlaywrightTest => {
38-
const { displayName } = test.context.config
43+
const { displayName, testEnvironmentOptions } = test.context.config
3944
const playwrightDisplayName = getDisplayName(browser, device)
4045
return {
4146
...test,
4247
context: {
4348
...test.context,
4449
config: {
4550
...test.context.config,
51+
testEnvironmentOptions: {
52+
...testEnvironmentOptions,
53+
[CONFIG_ENVIRONMENT_NAME]: config,
54+
},
4655
browserName: browser,
4756
wsEndpoint,
4857
device,
@@ -110,6 +119,7 @@ class PlaywrightRunner extends JestRunner {
110119
pwTests.push(
111120
getBrowserTest(
112121
test as JestPlaywrightTest,
122+
config,
113123
browser,
114124
wsEndpoint,
115125
device,
@@ -120,6 +130,7 @@ class PlaywrightRunner extends JestRunner {
120130
pwTests.push(
121131
getBrowserTest(
122132
test as JestPlaywrightTest,
133+
config,
123134
browser,
124135
wsEndpoint,
125136
null,
@@ -140,7 +151,11 @@ class PlaywrightRunner extends JestRunner {
140151
onFailure: OnTestFailure,
141152
options: TestRunnerOptions,
142153
): Promise<void> {
143-
const config = await readConfig(tests[0].context.config.rootDir)
154+
const { rootDir, testEnvironmentOptions } = tests[0].context.config
155+
const config = await readConfig(
156+
rootDir,
157+
testEnvironmentOptions[CONFIG_ENVIRONMENT_NAME],
158+
)
144159
const browserTests = await this.getTests(tests, config)
145160
if (config.collectCoverage) {
146161
await setupCoverage()

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { JestPlaywrightConfig, ConnectOptions } from '../types/global'
22

33
export const IMPORT_KIND_PLAYWRIGHT = 'playwright'
44

5+
export const CONFIG_ENVIRONMENT_NAME = 'jest-playwright'
6+
57
export const CHROMIUM = 'chromium'
68
export const FIREFOX = 'firefox'
79
export const WEBKIT = 'webkit'

src/global.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ const logMessage = ({
2626
export async function setup(
2727
jestConfig: JestConfig.GlobalConfig,
2828
): Promise<void> {
29+
// TODO It won't work if config doesn't exist in root directory or in jest.config.js file
2930
const config = await readConfig(jestConfig.rootDir)
3031

31-
// If we are in watch mode, - only setupServer() once.
32+
// If we are in watch mode - only setupServer() once.
3233
if (jestConfig.watch || jestConfig.watchAll) {
3334
if (didAlreadyRunInWatchMode) return
3435
didAlreadyRunInWatchMode = true

src/utils.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import * as Utils from './utils'
44
import { DEFAULT_CONFIG, CHROMIUM, FIREFOX } from './constants'
5-
import type { BrowserType } from '../types/global'
5+
import type { BrowserType, JestPlaywrightConfig } from '../types/global'
66

77
const {
88
readConfig,
@@ -35,7 +35,7 @@ describe('readConfig', () => {
3535
launchOptions: {
3636
headless: true,
3737
},
38-
browser: 'chromium',
38+
browsers: ['chromium'],
3939
contextOptions: {
4040
viewport: {
4141
width: 800,
@@ -52,6 +52,29 @@ describe('readConfig', () => {
5252
const config = await readConfig()
5353
expect(config).toMatchObject(configObject)
5454
})
55+
it('should overwrite config if the second param is passed', async () => {
56+
const configObject = {
57+
launchOptions: {
58+
headless: true,
59+
},
60+
browsers: ['chromium'],
61+
}
62+
jest.mock(
63+
path.join(__dirname, '..', 'jest-playwright.config.js'),
64+
() => ({
65+
launchOptions: {
66+
headless: true,
67+
},
68+
browsers: ['webkit'],
69+
}),
70+
{ virtual: true },
71+
)
72+
const config = await readConfig(
73+
process.cwd(),
74+
configObject as JestPlaywrightConfig,
75+
)
76+
expect(config).toMatchObject(configObject)
77+
})
5578
it('should overwrite with a custom configuration and spread the "launchOptions" and "contextOptions" setting', async () => {
5679
const configObject = {
5780
launchOptions: {

src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ export const getSkipFlag = (
197197

198198
export const readConfig = async (
199199
rootDir: string = process.cwd(),
200+
jestEnvConfig?: JestPlaywrightConfig,
200201
): Promise<JestPlaywrightConfig> => {
202+
if (jestEnvConfig) {
203+
return deepMerge<JestPlaywrightConfig>(DEFAULT_CONFIG, jestEnvConfig)
204+
}
201205
const hasCustomConfigPath = !!process.env.JEST_PLAYWRIGHT_CONFIG
202206
let fileExtension = 'js'
203207
if (process.env.npm_package_type === 'module') {

0 commit comments

Comments
 (0)