-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mmarkelov/jest-playwright
- Loading branch information
Showing
6 changed files
with
194 additions
and
58 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
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,97 @@ | ||
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, getExitCode, 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) | ||
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`) | ||
}) | ||
} else { | ||
const { status } = spawnSync( | ||
'node', | ||
[`node_modules/jest/bin/jest.js ${params}`], | ||
options, | ||
) | ||
console.log(`${getResultByStatus(status)} tests for ${browser}`) | ||
} | ||
} | ||
}): Promise<number | null> => | ||
new Promise(resolve => { | ||
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(getLogMessage(browser, status, device)) | ||
resolve(status) | ||
}) | ||
} else { | ||
const { status } = spawnSync( | ||
'node', | ||
[`node_modules/jest/bin/jest.js ${params}`], | ||
options, | ||
) | ||
console.log(getLogMessage(browser, status, device)) | ||
resolve(status) | ||
} | ||
}) | ||
|
||
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() | ||
let exitCodes: (number | null)[] = [] | ||
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 | ||
} | ||
exitCodes = await Promise.all( | ||
devices.map(device => | ||
exec({ sequence, browser: browserType, device, params }), | ||
), | ||
) | ||
} | ||
if (browsers.length) { | ||
if (devices.length) { | ||
const multipleCodes = await Promise.all( | ||
browsers.map(browser => | ||
Promise.all( | ||
devices.map(device => exec({ sequence, browser, device, params })), | ||
), | ||
), | ||
) | ||
exitCodes = multipleCodes.reduce((acc, val) => acc.concat(val), []) | ||
} else { | ||
exitCodes = await Promise.all( | ||
browsers.map(browser => exec({ sequence, browser, params })), | ||
) | ||
} | ||
} | ||
getExitCode(exitCodes) | ||
} | ||
|
||
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,39 @@ | ||
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` | ||
} | ||
|
||
export const getExitCode = (exitCodes: (number | null)[]): void => { | ||
if (exitCodes.every(code => code === 0)) { | ||
process.exit(0) | ||
} else { | ||
console.log('One of the test has not passed successfully') | ||
process.exit(1) | ||
} | ||
} |
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