Skip to content

Commit

Permalink
Improve logs for jest-playwright command (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkelov committed Mar 16, 2020
1 parent 416bbd6 commit 7228fab
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 70 deletions.
49 changes: 21 additions & 28 deletions src/PlaywrightEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ const resetBrowserCloseWatchdog = (): void => {
if (browserShutdownTimeout) clearTimeout(browserShutdownTimeout)
}

const logMessage = ({
message,
action,
}: {
message: string
action: string
}): void => {
console.log('')
console.error(message)
console.error(`\n☝️ You ${action} in jest-playwright.config.js`)
process.exit(1)
}

// Since there are no per-worker hooks, we have to setup a timer to
// close the browser.
//
Expand Down Expand Up @@ -68,19 +81,6 @@ class PlaywrightEnvironment extends NodeEnvironment {
super(config)
this._config = config
}
// Jest is not available here, so we have to reverse engineer
// the setTimeout function, see https://github.com/facebook/jest/blob/v23.1.0/packages/jest-runtime/src/index.js#L823
setTimeout(timeout: number): void {
if (this.global.jasmine) {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
this.global.jasmine.DEFAULT_TIMEOUT_INTERVAL = timeout
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
this.global[Symbol.for('TEST_TIMEOUT_SYMBOL')] = timeout
}
}

async setup(): Promise<void> {
resetBrowserCloseWatchdog()
Expand All @@ -104,20 +104,16 @@ class PlaywrightEnvironment extends NodeEnvironment {
await setup(config.server)
} catch (error) {
if (error.code === ERROR_TIMEOUT) {
console.log('')
console.error(error.message)
console.error(
`\n☝️ You can set "server.launchTimeout" in jest-playwright.config.js`,
)
process.exit(1)
logMessage({
message: error.message,
action: 'can set "server.launchTimeout"',
})
}
if (error.code === ERROR_NO_COMMAND) {
console.log('')
console.error(error.message)
console.error(
`\n☝️ You must set "server.command" in jest-playwright.config.js`,
)
process.exit(1)
logMessage({
message: error.message,
action: 'must set "server.command"',
})
}
throw error
}
Expand All @@ -135,9 +131,6 @@ class PlaywrightEnvironment extends NodeEnvironment {
this.global.page.on('pageerror', handleError)
this.global.jestPlaywright = {
debug: async (): Promise<void> => {
// eslint-disable-next-line no-eval
// Set timeout to 4 days
this.setTimeout(345600000)
// Run a debugger (in case Playwright has been launched with `{ devtools: true }`)
await this.global.page.evaluate(() => {
// eslint-disable-next-line no-debugger
Expand Down
13 changes: 8 additions & 5 deletions src/bin/testProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
readConfig,
readPackage,
} from '../utils'
import { checkCommand, getExitCode, getLogMessage } from './utils'
import { checkCommand, getDisplayName, getExitCode } from './utils'
import { BrowserType, CORE, PARALLEL, PLAYWRIGHT } from '../constants'

const getSpawnOptions = (
Expand Down Expand Up @@ -34,23 +34,26 @@ const exec = ({
}): Promise<number | null> =>
new Promise(resolve => {
const options = getSpawnOptions(browser, device)
const displayName = getDisplayName(browser, device)
if (sequence === PARALLEL) {
const process = spawn(
'node',
[`node_modules/jest/bin/jest.js ${params}`],
[
`node_modules/jest/bin/jest.js --displayName="${displayName}" ${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}`],
[
`node_modules/jest/bin/jest.js --displayName="${displayName}" ${params}`,
],
options,
)
console.log(getLogMessage(browser, status, device))
resolve(status)
}
})
Expand Down
35 changes: 7 additions & 28 deletions src/bin/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
checkCommand,
getResultByStatus,
getLogMessage,
getExitCode,
} from './utils'
import { checkCommand, getDisplayName, getExitCode } from './utils'
import { BrowserType } from '../constants'

describe('checkCommand', () => {
Expand All @@ -18,30 +13,14 @@ describe('checkCommand', () => {
})
})

describe('getResultByStatus', () => {
it('should return "Failed" if passed null', () => {
expect(getResultByStatus(null)).toBe('Failed')
describe('getDisplayName', () => {
it('should return right display name for passed browser', () => {
expect(getDisplayName('chromium', null)).toBe('browser: chromium')
})

it('should return "Failed" if passed code 1', () => {
expect(getResultByStatus(1)).toBe('Failed')
})

it('should return "Passed" if passed code 0', () => {
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',
it('should return right display name for passed browser and device', () => {
expect(getDisplayName('chromium', 'iPhone 6')).toBe(
'browser: chromium device: iPhone 6',
)
})
})
Expand Down
11 changes: 2 additions & 9 deletions src/bin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,11 @@ export const checkCommand = (
// devices.forEach(checkDeviceEnv)
}

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

export const getLogMessage = (
export const getDisplayName = (
browser: BrowserType,
status: number | null,
device: string | null,
): string => {
return `${getResultByStatus(status)} tests for browser: ${browser} ${
device ? `and device: ${device}` : ''
}\n\n`
return `browser: ${browser}${device ? ` device: ${device}` : ''}`
}

export const getExitCode = (exitCodes: (number | null)[]): void => {
Expand Down

0 comments on commit 7228fab

Please sign in to comment.