Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc3e75a5

Browse files
authored
Merge pull request#217 from coderoad/logs
Improved Test Logging
2 parents7cda121 +4268b39 commitc3e75a5

File tree

5 files changed

+97
-23
lines changed

5 files changed

+97
-23
lines changed

‎src/services/testRunner/formatOutput.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { ParserOutput, Fail } from './parser'
44
// export const formatSuccessOutput = (tap: ParserOutput): string => {}
55

66
exportconstformatFailOutput=(tap:ParserOutput):string=>{
7-
letoutput=`'TESTSFAILED\n`
7+
letoutput=`FAILED TESTS\n`
88
tap.failed.forEach((fail:Fail)=>{
9-
constdetails=fail.details ?`\n${fail.details}\n\n` :''
10-
output+=` ✘${fail.message}\n${details}`
9+
constdetails=fail.details ?`\n${fail.details}\n` :''
10+
constlogs=fail.logs ?`\n${fail.logs.join('\n')}\n` :''
11+
constresult=`${logs}${fail.message}\n${details}`
12+
output+=result
1113
})
1214
returnoutput
1315
}

‎src/services/testRunner/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ interface TestRunnerConfig {
2121
command:string
2222
}
2323

24+
constfailChannelName='CodeRoad (Tests)'
25+
constlogChannelName='CodeRoad (Logs)'
26+
2427
constcreateTestRunner=(config:TestRunnerConfig,callbacks:Callbacks)=>{
2528
returnasync(payload:Payload,onSuccess?:()=>void):Promise<void>=>{
2629
conststartTime=throttle()
@@ -52,25 +55,28 @@ const createTestRunner = (config: TestRunnerConfig, callbacks: Callbacks) => {
5255
const{ stdout, stderr}=result
5356

5457
consttap=parser(stdout||'')
58+
59+
displayOutput({channel:logChannelName,text:tap.logs.join('\n'),show:false})
60+
5561
if(stderr){
5662
// FAIL also trigger stderr
5763
if(stdout&&stdout.length&&!tap.ok){
5864
constfirstFailMessage=tap.failed[0].message
5965
callbacks.onFail(payload,firstFailMessage)
6066
constoutput=formatFailOutput(tap)
61-
displayOutput(output)
67+
displayOutput({channel:failChannelName,text:output,show:true})
6268
return
6369
}else{
6470
callbacks.onError(payload)
6571
// open terminal with error string
66-
displayOutput(stderr)
72+
displayOutput({channel:failChannelName,text:stderr,show:true})
6773
return
6874
}
6975
}
7076

7177
// PASS
7278
if(tap.ok){
73-
clearOutput()
79+
clearOutput(failChannelName)
7480
callbacks.onSuccess(payload)
7581
if(onSuccess){
7682
onSuccess()

‎src/services/testRunner/output.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import*asvscodefrom'vscode'
22

3-
letchannel:vscode.OutputChannel
3+
constchannels:{key:string;value:vscode.OutputChannel}|{}={}
44

55
constgetOutputChannel=(name:string):vscode.OutputChannel=>{
6-
if(!channel){
7-
channel=vscode.window.createOutputChannel(name)
6+
if(!channels[name]){
7+
channels[name]=vscode.window.createOutputChannel(name)
88
}
9-
returnchannel
9+
returnchannels[name]
1010
}
1111

12-
constoutputChannelName='CodeRoad Output'
12+
interfaceDisplayOutput{
13+
channel:string
14+
text:string
15+
show?:boolean
16+
}
1317

14-
exportconstdisplayOutput=(text:string)=>{
15-
constchannel=getOutputChannel(outputChannelName)
18+
exportconstdisplayOutput=(params:DisplayOutput)=>{
19+
constchannel=getOutputChannel(params.channel)
1620
channel.clear()
17-
channel.show(true)
18-
channel.append(text)
21+
channel.show(params.show||false)
22+
channel.append(params.text)
1923
}
2024

21-
exportconstclearOutput=()=>{
22-
constchannel=getOutputChannel(outputChannelName)
25+
exportconstclearOutput=(channelName:string)=>{
26+
constchannel=getOutputChannel(channelName)
2327
channel.show(false)
2428
channel.clear()
2529
channel.hide()

‎src/services/testRunner/parser.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('parser', () => {
66
1..1
77
ok 1 - Should pass
88
`
9-
expect(parser(example)).toEqual({ok:true,passed:[{message:'Should pass'}],failed:[]})
9+
expect(parser(example)).toEqual({ok:true,passed:[{message:'Should pass'}],failed:[],logs:[]})
1010
})
1111
test('should detect multiple successes',()=>{
1212
constexample=`
@@ -19,6 +19,7 @@ ok 2 - Should also pass
1919
ok:true,
2020
passed:[{message:'Should pass'},{message:'Should also pass'}],
2121
failed:[],
22+
logs:[],
2223
})
2324
})
2425
test('should detect failure if no tests passed',()=>{
@@ -141,4 +142,34 @@ at processImmediate (internal/timers.js:439:21)`)
141142
expect(result.failed[1].message).toBe('package.json should have a valid "description" key')
142143
expect(result.failed[1].details).toBe(`AssertionError [ERR_ASSERTION]: no "description" key provided`)
143144
})
145+
test('should capture logs',()=>{
146+
constexample=`
147+
1..2
148+
ok 1 package.json should have "express" installed
149+
log 1
150+
log 2
151+
not ok 2 server should log "Hello World"
152+
# AssertionError [ERR_ASSERTION]: "Hello World was not logged
153+
# at Context.<anonymous> (test/server.test.js:15:12)
154+
# at processImmediate (internal/timers.js:439:21)
155+
# tests 2
156+
# pass 1
157+
# fail 1
158+
# skip 0
159+
`
160+
expect(parser(example)).toEqual({
161+
ok:false,
162+
passed:[{message:'package.json should have "express" installed'}],
163+
failed:[
164+
{
165+
message:'server should log "Hello World"',
166+
details:`AssertionError [ERR_ASSERTION]: \"Hello World was not logged
167+
at Context.<anonymous> (test/server.test.js:15:12)
168+
at processImmediate (internal/timers.js:439:21)`,
169+
logs:['log 1','log 2'],
170+
},
171+
],
172+
logs:['log 1','log 2'],
173+
})
174+
})
144175
})

‎src/services/testRunner/parser.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
exportinterfaceFail{
22
message:string
33
details?:string
4+
logs?:string[]
5+
}
6+
7+
exportinterfacePass{
8+
message:string
9+
logs?:string[]
410
}
511

612
exportinterfaceParserOutput{
713
ok:boolean
8-
passed:Array<{message:string}>
9-
failed:Array<Fail>
14+
passed:Pass[]
15+
failed:Fail[]
16+
logs:string[]
1017
}
1118

1219
constr={
20+
start:/^1\.\.[0-9]+$/,
1321
fail:/^notok\d+\s(\-\s)?(.+)+$/,
1422
pass:/^ok\d+\s(\-\s)?(.+)+$/,
1523
details:/^#\s{2}(.+)$/,
24+
ignore:/^#\s+(tests|pass|fail|skip)\s+[0-9]+$/,
1625
}
1726

1827
constdetect=(type:'fail'|'pass'|'details',text:string)=>r[type].exec(text)
1928

2029
constparser=(text:string):ParserOutput=>{
21-
constlines=text.split('\n')
30+
constlineList=text.split('\n')
31+
// start after 1..n output
32+
conststartingPoint=lineList.findIndex((t)=>t.match(r.start))
33+
constlines=lineList.slice(startingPoint+1)
2234

2335
constresult:ParserOutput={
2436
ok:true,
2537
passed:[],
2638
failed:[],
39+
logs:[],
2740
}
2841

2942
// temporary holder of error detail strings
3043
letcurrentDetails:string|null=null
44+
letlogs:string[]=[]
3145

3246
constaddCurrentDetails=()=>{
3347
constfailLength:number=result.failed.length
@@ -44,7 +58,12 @@ const parser = (text: string): ParserOutput => {
4458
// be optimistic! check for success
4559
constisPass=detect('pass',line)
4660
if(!!isPass){
47-
result.passed.push({message:isPass[2].trim()})
61+
constpass:Pass={message:isPass[2].trim()}
62+
if(logs.length){
63+
pass.logs=logs
64+
logs=[]
65+
}
66+
result.passed.push(pass)
4867
addCurrentDetails()
4968
continue
5069
}
@@ -54,7 +73,12 @@ const parser = (text: string): ParserOutput => {
5473
if(!!isFail){
5574
result.ok=false
5675
addCurrentDetails()
57-
result.failed.push({message:isFail[2].trim()})
76+
constfail:Fail={message:isFail[2].trim()}
77+
if(logs.length){
78+
fail.logs=logs
79+
logs=[]
80+
}
81+
result.failed.push(fail)
5882
continue
5983
}
6084

@@ -68,6 +92,13 @@ const parser = (text: string): ParserOutput => {
6892
//@ts-ignore ignore as it must be a string
6993
currentDetails+=`\n${lineDetails}`
7094
}
95+
continue
96+
}
97+
98+
if(!r.ignore.exec(line)){
99+
// must be a log, associate with the next test
100+
logs.push(line)
101+
result.logs.push(line)
71102
}
72103
}
73104
addCurrentDetails()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp