Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/PlaywrightEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
BrowserContextOptions,
Page,
} from 'playwright-core'
import { Event } from 'jest-circus'
import type {
BrowserType,
ConfigDeviceType,
Expand Down Expand Up @@ -198,6 +199,16 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
return page
}

async _setCollectCoverage(context: BrowserContext) {
await context.exposeFunction('reportCodeCoverage', saveCoverageToFile)
await context.addInitScript(() =>
window.addEventListener('beforeunload', () => {
// @ts-ignore
reportCodeCoverage(window.__coverage__)
}),
)
}

async setup(): Promise<void> {
const { wsEndpoint, browserName, testEnvironmentOptions } = this._config
this._jestPlaywrightConfig = testEnvironmentOptions[
Expand Down Expand Up @@ -256,16 +267,7 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
? browserOrContext
: await this.global.browser.newContext(contextOptions)
if (collectCoverage) {
await (this.global.context as BrowserContext).exposeFunction(
'reportCodeCoverage',
saveCoverageToFile,
)
await (this.global.context as BrowserContext).addInitScript(() =>
window.addEventListener('beforeunload', () => {
// @ts-ignore
reportCodeCoverage(window.__coverage__)
}),
)
await this._setCollectCoverage(this.global.context as BrowserContext)
}
this.global.page = await this._setNewPageInstance()
}
Expand Down Expand Up @@ -345,6 +347,28 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
}
}

async handleTestEvent(event: Event) {
const { browserName } = this._config
const { collectCoverage, haveSkippedTests } = this._jestPlaywrightConfig
const browserType = getBrowserType(browserName)
const { instance, devices } = getPlaywrightInstance(browserType)
const contextOptions = this._getContextOptions(devices)
if (haveSkippedTests && event.name === 'test_fn_start') {
this.global.browser = await getBrowserPerProcess(
instance as GenericBrowser,
browserType,
this._jestPlaywrightConfig,
)
this.global.context = await this.global.browser.newContext(
contextOptions,
)
if (collectCoverage) {
await this._setCollectCoverage(this.global.context)
}
this.global.page = await this._setNewPageInstance()
}
}

async teardown(): Promise<void> {
const { browser, context, page } = this.global
const { collectCoverage } = this._jestPlaywrightConfig
Expand Down
8 changes: 8 additions & 0 deletions src/PlaywrightRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
DEFAULT_TEST_PLAYWRIGHT_TIMEOUT,
CONFIG_ENVIRONMENT_NAME,
SERVER,
LAUNCH,
} from './constants'
import { setupCoverage, mergeCoverage } from './coverage'
import { GenericBrowser } from '../types/global'
Expand Down Expand Up @@ -105,6 +106,7 @@ const getJestTimeout = (configTimeout?: number) => {

class PlaywrightRunner extends JestRunner {
browser2Server: Partial<Record<string, BrowserServer>>
config: JestConfig.GlobalConfig
constructor(
globalConfig: JestConfig.GlobalConfig,
context: TestRunnerContext,
Expand All @@ -114,6 +116,7 @@ class PlaywrightRunner extends JestRunner {
config.testTimeout = getJestTimeout(config.testTimeout)
super(config, context)
this.browser2Server = {}
this.config = config
}

async launchServer(
Expand Down Expand Up @@ -202,6 +205,11 @@ class PlaywrightRunner extends JestRunner {
rootDir,
testEnvironmentOptions[CONFIG_ENVIRONMENT_NAME] as JestPlaywrightConfig,
)
if (this.config.testNamePattern) {
config.launchType = LAUNCH
config.skipInitialization = true
config.haveSkippedTests = true
}
const browserTests = await this.getTests(tests, config)
if (config.collectCoverage) {
await setupCoverage()
Expand Down
3 changes: 2 additions & 1 deletion src/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const fsAsync = fs.promises

// @ts-ignore
import NYC from 'nyc'
import { PACKAGE_NAME } from './constants'

const NYC_DIR = '.nyc_output'
const COV_MERGE_DIR = path.join(NYC_DIR, 'merge')
Expand Down Expand Up @@ -37,7 +38,7 @@ export const saveCoverageOnPage = async (
): Promise<void> => {
if (!collectCoverage) {
console.warn(
'jest-playwright: saveCoverage was called but collectCoverage is not true in jest-playwright.js',
`${PACKAGE_NAME}: saveCoverage was called but collectCoverage is not true in config file`,
)
return
}
Expand Down
1 change: 1 addition & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export type ServerOptions = JestProcessManagerOptions & {
}

export interface JestPlaywrightConfig {
haveSkippedTests?: boolean
skipInitialization?: boolean
debugOptions?: JestPlaywrightConfig
launchType?: LaunchType
Expand Down