Skip to content

Commit

Permalink
Merge 531b28d into 9c3fa21
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkelov committed Feb 29, 2020
2 parents 9c3fa21 + 531b28d commit b5daaf9
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 41 deletions.
53 changes: 43 additions & 10 deletions src/bin/testProcess.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,83 @@
import { spawn, spawnSync, SpawnSyncOptions } from 'child_process'
import { readConfig } from '../utils'
import { checkBrowsers, getResultByStatus } from './utils'
import { PARALLEL, BrowserType } from '../constants'
import {
checkBrowserEnv,
getBrowserType,
readConfig,
readPackage,
} from '../utils'
import { checkCommand, getLogMessage } from './utils'
import { BrowserType, CORE, PARALLEL, PLAYWRIGHT } from '../constants'

const getSpawnOptions = (browser: BrowserType): SpawnSyncOptions => ({
const getSpawnOptions = (
browser: BrowserType,
device: string | null,
): SpawnSyncOptions => ({
stdio: 'inherit',
shell: true,
env: {
...process.env,
BROWSER: browser,
...(device ? { DEVICE: device } : {}),
},
})

const exec = ({
sequence,
browser,
device = null,
params,
}: {
sequence: string
browser: BrowserType
device?: string | null
params: string[]
}): void => {
const options = getSpawnOptions(browser)
const options = getSpawnOptions(browser, device)
if (sequence === PARALLEL) {
const process = spawn(
'node',
[`node_modules/jest/bin/jest.js ${params}`],
options,
)
process.on('close', status => {
console.log(`${getResultByStatus(status)} tests for ${browser}\n\n`)
console.log(getLogMessage(browser, status, device))
})
} else {
const { status } = spawnSync(
'node',
[`node_modules/jest/bin/jest.js ${params}`],
options,
)
console.log(`${getResultByStatus(status)} tests for ${browser}`)
console.log(getLogMessage(browser, status, device))
}
}

const runner = async (sequence: string, params: string[]): Promise<void> => {
const { browsers = [] } = await readConfig()
checkBrowsers(browsers)
browsers.forEach(browser => exec({ sequence, browser, params }))
const { browsers = [], devices = [] } = await readConfig()
checkCommand(browsers, devices)
if (!browsers.length && devices.length) {
let browserType: BrowserType
const browser = await readPackage()
if (browser === PLAYWRIGHT || browser === CORE) {
const config = await readConfig()
browserType = getBrowserType(config)
checkBrowserEnv(browserType)
} else {
browserType = browser
}
devices.forEach(device =>
exec({ sequence, browser: browserType, device, params }),
)
}
if (browsers.length) {
if (devices.length) {
browsers.forEach(browser =>
devices.forEach(device => exec({ sequence, browser, device, params })),
)
} else {
browsers.forEach(browser => exec({ sequence, browser, params }))
}
}
}

export default runner
34 changes: 21 additions & 13 deletions src/bin/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { checkBrowsers, getResultByStatus } from './utils'
import { checkCommand, getResultByStatus, getLogMessage } from './utils'
import { BrowserType } from '../constants'

describe('checkBrowsers', () => {
it('should throw an error without arguments', () => {
expect(() => checkBrowsers()).toThrow(
'You should define browsers with your jest-playwright.config.js',
)
})
it('should throw an error when passed empty array', () => {
expect(() => checkBrowsers([])).toThrow(
'You should define browsers with your jest-playwright.config.js',
describe('checkCommand', () => {
it('should throw an error with empty browsers and devices', () => {
expect(() => checkCommand([], [])).toThrow(
'You should define browsers or devices with your jest-playwright.config.js',
)
})

it('should throw an error when passed wrong browser', () => {
expect(() =>
checkBrowsers(['chromium', 'unknown' as BrowserType]),
).toThrow()
expect(() => checkCommand(['unknown' as BrowserType], [])).toThrow()
})
})

Expand All @@ -32,3 +26,17 @@ describe('getResultByStatus', () => {
expect(getResultByStatus(0)).toBe('Passed')
})
})

describe('getLogMessage', () => {
it('should return right log', () => {
expect(getLogMessage('chromium', 0, null)).toBe(
'Passed tests for browser: chromium \n\n',
)
})

it('should return right log', () => {
expect(getLogMessage('chromium', 1, 'iPhone 6')).toBe(
'Failed tests for browser: chromium and device: iPhone 6\n\n',
)
})
})
21 changes: 18 additions & 3 deletions src/bin/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
import { checkBrowserEnv } from '../utils'
import { BrowserType } from '../constants'

export const checkBrowsers = (browsers?: BrowserType[]): void => {
if (!browsers || !browsers.length) {
export const checkCommand = (
browsers: BrowserType[],
devices: string[],
): void => {
if (!browsers.length && !devices.length) {
throw new Error(
'You should define browsers with your jest-playwright.config.js',
'You should define browsers or devices with your jest-playwright.config.js',
)
}
browsers.forEach(checkBrowserEnv)
// TODO Add check for devices
// devices.forEach(checkDeviceEnv)
}

export const getResultByStatus = (status: number | null): string => {
return status !== 0 ? 'Failed' : 'Passed'
}

export const getLogMessage = (
browser: BrowserType,
status: number | null,
device: string | null,
): string => {
return `${getResultByStatus(status)} tests for browser: ${browser} ${
device ? `and device: ${device}` : ''
}\n\n`
}
17 changes: 13 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import { LaunchOptions } from 'playwright-core/lib/server/browserType'
import { BrowserContextOptions } from 'playwright-core/lib/browserContext'
import { JestDevServerOptions } from 'jest-dev-server'

export type BrowserType = 'chromium' | 'firefox' | 'webkit'
export const CORE = 'core'
export const PLAYWRIGHT = 'playwright'

export const CHROMIUM: BrowserType = 'chromium'
export const FIREFOX: BrowserType = 'firefox'
export const WEBKIT: BrowserType = 'webkit'
export const CHROMIUM = 'chromium'
export const FIREFOX = 'firefox'
export const WEBKIT = 'webkit'

export type BrowserType = typeof CHROMIUM | typeof FIREFOX | typeof WEBKIT

export type PlaywrightRequireType =
| BrowserType
| typeof PLAYWRIGHT
| typeof CORE

export const PARALLEL = '--parallel'

Expand All @@ -17,6 +25,7 @@ export interface Config {
browser?: BrowserType
browsers?: BrowserType[]
device?: string
devices?: string[]
server?: JestDevServerOptions
}

Expand Down
23 changes: 12 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ import fs from 'fs'
import path from 'path'
import { promisify } from 'util'
import {
BrowserType,
CHROMIUM,
Config,
CORE,
DEFAULT_CONFIG,
FIREFOX,
PLAYWRIGHT,
PlaywrightRequireType,
WEBKIT,
DEFAULT_CONFIG,
Config,
BrowserType,
} from './constants'
import { BrowserType as PlayWrightBrowserType } from 'playwright'

const exists = promisify(fs.exists)

type PlaywrightRequireType = BrowserType | 'core' | 'playwright'

const checkDependencies = (
dependencies: Record<string, string>,
): PlaywrightRequireType | null => {
if (!dependencies) return null
if (dependencies.playwright) return 'playwright'
if (dependencies['playwright-core']) return 'core'
if (dependencies.playwright) return PLAYWRIGHT
if (dependencies[`playwright-${CORE}`]) return CORE
if (dependencies[`playwright-${CHROMIUM}`]) return CHROMIUM
if (dependencies[`playwright-${FIREFOX}`]) return FIREFOX
if (dependencies[`playwright-${WEBKIT}`]) return WEBKIT
Expand Down Expand Up @@ -68,7 +69,7 @@ export const readPackage = async (): Promise<PlaywrightRequireType> => {
const packageConfig = await require(absConfigPath)
// for handling the local tests
if (packageConfig.name === 'jest-playwright-preset') {
return 'core'
return CORE
}
const playwright =
checkDependencies(packageConfig.dependencies) ||
Expand All @@ -83,11 +84,11 @@ export const getPlaywrightInstance = async (
browserType: BrowserType,
): Promise<PlayWrightBrowserType> => {
const playwrightPackage = await readPackage()
if (playwrightPackage === 'playwright') {
if (playwrightPackage === PLAYWRIGHT) {
return require('playwright')[browserType]
}
if (playwrightPackage === 'core') {
const browser = require('playwright-core')[browserType]
if (playwrightPackage === CORE) {
const browser = require(`playwright-${CORE}`)[browserType]
await browser.downloadBrowserIfNeeded()
return browser
}
Expand Down

0 comments on commit b5daaf9

Please sign in to comment.