Skip to content

Make new identificator for browser2server #568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2021
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
14 changes: 8 additions & 6 deletions src/PlaywrightRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
getBrowserType,
getDeviceBrowserType,
deepMerge,
generateKey,
} from './utils'
import {
DEFAULT_TEST_PLAYWRIGHT_TIMEOUT,
Expand Down Expand Up @@ -95,7 +96,7 @@ const getDevices = (
}

class PlaywrightRunner extends JestRunner {
browser2Server: Partial<Record<BrowserType, BrowserServer>>
browser2Server: Partial<Record<string, BrowserServer>>
constructor(
globalConfig: JestConfig.GlobalConfig,
context: TestRunnerContext,
Expand All @@ -114,13 +115,14 @@ class PlaywrightRunner extends JestRunner {
instance: GenericBrowser,
): Promise<WsEndpointType> {
const { launchType, launchOptions, skipInitialization } = config
const key = generateKey(browser, config)
if (!skipInitialization && launchType === SERVER && wsEndpoint === null) {
if (!this.browser2Server[browser]) {
if (!this.browser2Server[key]) {
const options = getBrowserOptions(browser, launchOptions)
this.browser2Server[browser] = await instance.launchServer(options)
this.browser2Server[key] = await instance.launchServer(options)
}
}
return wsEndpoint || this.browser2Server[browser]?.wsEndpoint() || null
return wsEndpoint || this.browser2Server[key]?.wsEndpoint() || null
}

async getTests(tests: Test[], config: JestPlaywrightConfig): Promise<Test[]> {
Expand Down Expand Up @@ -216,8 +218,8 @@ class PlaywrightRunner extends JestRunner {
options.serial ? '_createInBandTestRun' : '_createParallelTestRun'
](browserTests, watcher, onStart, onResult, onFailure)

for (const browser in this.browser2Server) {
await this.browser2Server[browser as BrowserType]!.close()
for (const key in this.browser2Server) {
await this.browser2Server[key]!.close()
}
if (config.collectCoverage) {
await mergeCoverage()
Expand Down
27 changes: 27 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
getSkipFlag,
getBrowserOptions,
getDeviceBrowserType,
generateKey,
} = Utils

beforeEach(() => {
Expand Down Expand Up @@ -141,6 +142,32 @@ describe('readConfig', () => {
})
})

describe('generateKey', () => {
const config = {
launchOptions: {
headless: false,
executablePath: '/usr/bin/microsoft-edge',
},
} as JestPlaywrightConfig
it('should generate same key for same objects', () => {
const key1 = generateKey('chromium', config)
const key2 = generateKey('chromium', config)
expect(key1).toBe(key2)
})

it('should generate different key for different objects', () => {
const chromiumConfig = {
launchOptions: {
headless: false,
executablePath: '/usr/bin/google-chrome-stable',
},
} as JestPlaywrightConfig
const key1 = generateKey('chromium', config)
const key2 = generateKey('chromium', chromiumConfig)
expect(key1).not.toBe(key2)
})
})

describe('getDisplayName', () => {
it('should return right display name for passed browser', () => {
expect(getDisplayName('chromium', null)).toBe('browser: chromium')
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export const getBrowserType = (browser?: BrowserType): BrowserType => {
return browser || CHROMIUM
}

export const generateKey = (
browser: BrowserType,
config: JestPlaywrightConfig,
): string => `${browser}${JSON.stringify(config)}`

export const getDeviceBrowserType = (
device: ConfigDeviceType,
availableDevices: Playwright['devices'],
Expand Down