Skip to content

Improved Test Logging #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/services/testRunner/formatOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { ParserOutput, Fail } from './parser'
// export const formatSuccessOutput = (tap: ParserOutput): string => {}

export const formatFailOutput = (tap: ParserOutput): string => {
let output = `'TESTS FAILED\n`
let output = `FAILED TESTS\n`
tap.failed.forEach((fail: Fail) => {
const details = fail.details ? `\n${fail.details}\n\n` : ''
output += ` ✘ ${fail.message}\n${details}`
const details = fail.details ? `\n${fail.details}\n` : ''
const logs = fail.logs ? `\n${fail.logs.join('\n')}\n` : ''
const result = `${logs} ✘ ${fail.message}\n${details}`
output += result
})
return output
}
12 changes: 9 additions & 3 deletions src/services/testRunner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ interface TestRunnerConfig {
command: string
}

const failChannelName = 'CodeRoad (Tests)'
const logChannelName = 'CodeRoad (Logs)'

const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks) => {
return async (payload: Payload, onSuccess?: () => void): Promise<void> => {
const startTime = throttle()
Expand Down Expand Up @@ -52,25 +55,28 @@ const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks) => {
const { stdout, stderr } = result

const tap = parser(stdout || '')

displayOutput({ channel: logChannelName, text: tap.logs.join('\n'), show: false })

if (stderr) {
// FAIL also trigger stderr
if (stdout && stdout.length && !tap.ok) {
const firstFailMessage = tap.failed[0].message
callbacks.onFail(payload, firstFailMessage)
const output = formatFailOutput(tap)
displayOutput(output)
displayOutput({ channel: failChannelName, text: output, show: true })
return
} else {
callbacks.onError(payload)
// open terminal with error string
displayOutput(stderr)
displayOutput({ channel: failChannelName, text: stderr, show: true })
return
}
}

// PASS
if (tap.ok) {
clearOutput()
clearOutput(failChannelName)
callbacks.onSuccess(payload)
if (onSuccess) {
onSuccess()
Expand Down
26 changes: 15 additions & 11 deletions src/services/testRunner/output.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import * as vscode from 'vscode'

let channel: vscode.OutputChannel
const channels: { key: string; value: vscode.OutputChannel } | {} = {}

const getOutputChannel = (name: string): vscode.OutputChannel => {
if (!channel) {
channel = vscode.window.createOutputChannel(name)
if (!channels[name]) {
channels[name] = vscode.window.createOutputChannel(name)
}
return channel
return channels[name]
}

const outputChannelName = 'CodeRoad Output'
interface DisplayOutput {
channel: string
text: string
show?: boolean
}

export const displayOutput = (text: string) => {
const channel = getOutputChannel(outputChannelName)
export const displayOutput = (params: DisplayOutput) => {
const channel = getOutputChannel(params.channel)
channel.clear()
channel.show(true)
channel.append(text)
channel.show(params.show || false)
channel.append(params.text)
}

export const clearOutput = () => {
const channel = getOutputChannel(outputChannelName)
export const clearOutput = (channelName: string) => {
const channel = getOutputChannel(channelName)
channel.show(false)
channel.clear()
channel.hide()
Expand Down
33 changes: 32 additions & 1 deletion src/services/testRunner/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('parser', () => {
1..1
ok 1 - Should pass
`
expect(parser(example)).toEqual({ ok: true, passed: [{ message: 'Should pass' }], failed: [] })
expect(parser(example)).toEqual({ ok: true, passed: [{ message: 'Should pass' }], failed: [], logs: [] })
})
test('should detect multiple successes', () => {
const example = `
Expand All @@ -19,6 +19,7 @@ ok 2 - Should also pass
ok: true,
passed: [{ message: 'Should pass' }, { message: 'Should also pass' }],
failed: [],
logs: [],
})
})
test('should detect failure if no tests passed', () => {
Expand Down Expand Up @@ -141,4 +142,34 @@ at processImmediate (internal/timers.js:439:21)`)
expect(result.failed[1].message).toBe('package.json should have a valid "description" key')
expect(result.failed[1].details).toBe(`AssertionError [ERR_ASSERTION]: no "description" key provided`)
})
test('should capture logs', () => {
const example = `
1..2
ok 1 package.json should have "express" installed
log 1
log 2
not ok 2 server should log "Hello World"
# AssertionError [ERR_ASSERTION]: "Hello World was not logged
# at Context.<anonymous> (test/server.test.js:15:12)
# at processImmediate (internal/timers.js:439:21)
# tests 2
# pass 1
# fail 1
# skip 0
`
expect(parser(example)).toEqual({
ok: false,
passed: [{ message: 'package.json should have "express" installed' }],
failed: [
{
message: 'server should log "Hello World"',
details: `AssertionError [ERR_ASSERTION]: \"Hello World was not logged
at Context.<anonymous> (test/server.test.js:15:12)
at processImmediate (internal/timers.js:439:21)`,
logs: ['log 1', 'log 2'],
},
],
logs: ['log 1', 'log 2'],
})
})
})
41 changes: 36 additions & 5 deletions src/services/testRunner/parser.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
export interface Fail {
message: string
details?: string
logs?: string[]
}

export interface Pass {
message: string
logs?: string[]
}

export interface ParserOutput {
ok: boolean
passed: Array<{ message: string }>
failed: Array<Fail>
passed: Pass[]
failed: Fail[]
logs: string[]
}

const r = {
start: /^1\.\.[0-9]+$/,
fail: /^not ok \d+\s(\-\s)?(.+)+$/,
pass: /^ok \d+\s(\-\s)?(.+)+$/,
details: /^#\s{2}(.+)$/,
ignore: /^#\s+(tests|pass|fail|skip)\s+[0-9]+$/,
}

const detect = (type: 'fail' | 'pass' | 'details', text: string) => r[type].exec(text)

const parser = (text: string): ParserOutput => {
const lines = text.split('\n')
const lineList = text.split('\n')
// start after 1..n output
const startingPoint = lineList.findIndex((t) => t.match(r.start))
const lines = lineList.slice(startingPoint + 1)

const result: ParserOutput = {
ok: true,
passed: [],
failed: [],
logs: [],
}

// temporary holder of error detail strings
let currentDetails: string | null = null
let logs: string[] = []

const addCurrentDetails = () => {
const failLength: number = result.failed.length
Expand All @@ -44,7 +58,12 @@ const parser = (text: string): ParserOutput => {
// be optimistic! check for success
const isPass = detect('pass', line)
if (!!isPass) {
result.passed.push({ message: isPass[2].trim() })
const pass: Pass = { message: isPass[2].trim() }
if (logs.length) {
pass.logs = logs
logs = []
}
result.passed.push(pass)
addCurrentDetails()
continue
}
Expand All @@ -54,7 +73,12 @@ const parser = (text: string): ParserOutput => {
if (!!isFail) {
result.ok = false
addCurrentDetails()
result.failed.push({ message: isFail[2].trim() })
const fail: Fail = { message: isFail[2].trim() }
if (logs.length) {
fail.logs = logs
logs = []
}
result.failed.push(fail)
continue
}

Expand All @@ -68,6 +92,13 @@ const parser = (text: string): ParserOutput => {
// @ts-ignore ignore as it must be a string
currentDetails += `\n${lineDetails}`
}
continue
}

if (!r.ignore.exec(line)) {
// must be a log, associate with the next test
logs.push(line)
result.logs.push(line)
}
}
addCurrentDetails()
Expand Down