Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
feat: redirect console
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Dec 19, 2021
1 parent c49e082 commit 36dd362
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/peeky-runner/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface WorkerRemoteMethods {
onTestError: (suiteId: string, testId: string, duration: number, error: TestErrorData) => void
onTestSuccess: (suiteId: string, testId: string, duration: number) => void
transform: (id: string) => Promise<TransformResult>
onLog: (suiteId: string, testId: string, type: 'stdout' | 'stderr', text: string) => void
}

export interface TestErrorData {
Expand Down
8 changes: 8 additions & 0 deletions packages/peeky-runner/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ export async function setupRunner (options: RunnerOptions) {
const test = suite.tests.find(t => t.id === testId)
consola.log(chalk.green(`${chalk.bgGreenBright.black.bold(' PASS ')} ${suite.title}${chalk.bold(test.title)} ${chalk.grey(`(${formatDurationToString(duration)})`)}`))
},

onLog: (suiteId, testId, type, text) => {
const suite = suiteMap[suiteId]
const test = suite?.tests.find(t => t.id === testId)
consola.log(chalk.dim(`\n[${type}] ${test ? `${suite.title}${chalk.bold(test.title)}` : 'unknown test'}\n`))
process[type].write(text)
process[type].write('\n')
},
}, handleMessage)

const result = await pool.run({
Expand Down
26 changes: 26 additions & 0 deletions packages/peeky-runner/src/runtime/console.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Console } from 'console'
import { Writable } from 'stream'
import { currentSuite, currentTest } from './global-context.js'
import { toMainThread } from './message.js'

export function setupConsole () {
function createWritable (type: 'stdout' | 'stderr') {
return new Writable({
write: (chunk, enconding, callback) => {
toMainThread().onLog(currentSuite?.id, currentTest?.id, type, String(chunk))
callback()
},
})
}
const stdout = createWritable('stdout')
const stderr = createWritable('stderr')

const console = new Console({
stdout,
stderr,
colorMode: true,
groupIndentation: 2,
})

globalThis.console = console
}
12 changes: 12 additions & 0 deletions packages/peeky-runner/src/runtime/global-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Test, TestSuite } from '../types.js'

export let currentSuite: TestSuite
export let currentTest: Test

export function setCurrentSuite (suite: TestSuite) {
currentSuite = suite
}

export function setCurrentTest (test: Test) {
currentTest = test
}
8 changes: 8 additions & 0 deletions packages/peeky-runner/src/runtime/run-tests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { basename } from 'path'
import { performance } from 'perf_hooks'
import type { Context, Test } from '../types'
import { setCurrentSuite, setCurrentTest } from './global-context.js'
import { toMainThread } from './message.js'

export async function runTests (ctx: Context) {
const { default: sinon } = await import('sinon')

for (const suite of ctx.suites) {
setCurrentSuite(suite)

let testsToRun: Test[]
const onlyTests = suite.tests.filter(t => t.flag === 'only')
if (onlyTests.length) {
Expand Down Expand Up @@ -35,6 +38,7 @@ export async function runTests (ctx: Context) {
}

for (const test of testsToRun) {
setCurrentTest(test)
sinon.restore()

for (const handler of suite.beforeEachHandlers) {
Expand Down Expand Up @@ -65,6 +69,8 @@ export async function runTests (ctx: Context) {
for (const handler of suite.afterEachHandlers) {
await handler()
}

setCurrentTest(null)
}

for (const handler of suite.afterAllHandlers) {
Expand All @@ -83,5 +89,7 @@ export async function runTests (ctx: Context) {
testErrors: suite.testErrors,
otherErrors: suite.otherErrors,
}, performance.now() - suiteTime)

setCurrentSuite(null)
}
}
3 changes: 3 additions & 0 deletions packages/peeky-runner/src/runtime/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PeekyConfig, setupConfigLoader } from '@peeky/config'
import { setupConsole } from './console.js'

let initialized = false
export let baseConfig: PeekyConfig
Expand All @@ -12,5 +13,7 @@ export async function setupWorker () {
await configLoader.destroy()
baseConfig = config

setupConsole()

initialized = true
}

0 comments on commit 36dd362

Please sign in to comment.