Skip to content

Commit

Permalink
Merge 3494ad4 into 44f07ff
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkelov committed Feb 26, 2020
2 parents 44f07ff + 3494ad4 commit 4705f49
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/bin/testProcess.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
import { spawn, spawnSync, SpawnSyncOptions } from 'child_process'
import { readConfig } from '../utils'
import { checkBrowsers, getResultByStatus } from './utils'
import { checkBrowsers, getLogMessage } from './utils'
import { PARALLEL, BrowserType } 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/.bin/jest ${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/.bin/jest ${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()
const { browsers = [], devices = [] } = await readConfig()
checkBrowsers(browsers)
browsers.forEach(browser => exec({ sequence, browser, params }))
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
16 changes: 15 additions & 1 deletion src/bin/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkBrowsers, getResultByStatus } from './utils'
import { checkBrowsers, getResultByStatus, getLogMessage } from './utils'
import { BrowserType } from '../constants'

describe('checkBrowsers', () => {
Expand Down Expand Up @@ -32,3 +32,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',
)
})
})
10 changes: 10 additions & 0 deletions src/bin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ export const checkBrowsers = (browsers?: BrowserType[]): void => {
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`
}
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Config {
browser?: BrowserType
browsers?: BrowserType[]
device?: string
devices?: string[]
server?: JestDevServerOptions
}

Expand Down

0 comments on commit 4705f49

Please sign in to comment.