-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters