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

Commita92f866

Browse files
committed
setup Test Runner
1 parentab13f52 commita92f866

File tree

4 files changed

+87
-153
lines changed

4 files changed

+87
-153
lines changed

‎src/actions/runTest/channel.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import*asvscodefrom'vscode'
2+
3+
letchannel:vscode.OutputChannel
4+
5+
exportconstgetOutputChannel=(name:string):vscode.OutputChannel=>{
6+
if(!channel){
7+
channel=vscode.window.createOutputChannel(name)
8+
}
9+
returnchannel
10+
}

‎src/actions/runTest/index.ts

Lines changed: 0 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +0,0 @@
1-
import*asvscodefrom'vscode'
2-
importnodefrom'../../services/node'
3-
4-
// TODO: use tap parser to make it easier to support other test runners
5-
6-
// ensure only latest run_test action is taken
7-
letcurrentId=0
8-
9-
// quick solution to prevent processing multiple results
10-
// NOTE: may be possible to kill child process early
11-
constshouldExitEarly=(processId:number):boolean=>{
12-
returncurrentId!==processId
13-
}
14-
15-
letchannel:vscode.OutputChannel
16-
17-
constgetOutputChannel=(name:string):vscode.OutputChannel=>{
18-
if(!channel){
19-
channel=vscode.window.createOutputChannel(name)
20-
}
21-
returnchannel
22-
}
23-
24-
interfaceCallbacks{
25-
onSuccess():void
26-
onFail():void
27-
onRun():void
28-
onError():void
29-
}
30-
31-
interfaceTestRunnerConfig{
32-
command:string
33-
parser(output:string):boolean
34-
}
35-
36-
exportconstcreateTestRunner=(config:TestRunnerConfig,callbacks:Callbacks)=>{
37-
38-
constoutputChannelName='TEST_OUTPUT'
39-
40-
return{
41-
run(){
42-
console.log('------------------- run test ------------------')
43-
constprocessId=++currentId
44-
callbacks.onRun()
45-
46-
try{
47-
const{stdout}=awaitnode.exec(config.command)
48-
}
49-
}
50-
}
51-
}
52-
53-
asyncfunctionrunTest({onSuccess, onFail, onRun, onError}:Callbacks):Promise<void>{
54-
55-
56-
57-
58-
// TODO: verify test runner for args
59-
// jest CLI docs https://jestjs.io/docs/en/cli
60-
// const testArgs = [
61-
// '--json',
62-
// '--onlyChanged',
63-
// '--env=node',
64-
// '--maxConcurrency=4',
65-
// '--maxWorkers=4'
66-
// ]
67-
68-
constcommandLine=`npm test --${testArgs.join(' ')}`
69-
70-
try{
71-
// capture position early on test start
72-
// in case position changes
73-
const{stdout}=awaitnode.exec(commandLine)
74-
if(shouldExitEarly(processId)){
75-
// exit early
76-
return
77-
}
78-
79-
if(stdout){
80-
constlines=stdout.split(/\r{0,1}\n/)
81-
for(constlineoflines){
82-
if(line.length>0){
83-
constregExp=/^{\"numFailedTestSuites/
84-
constmatches=regExp.exec(line)
85-
if(matches&&matches.length){
86-
constresult=JSON.parse(line)
87-
88-
if(result.success){
89-
if(shouldExitEarly(processId)){
90-
// exit early
91-
return
92-
}
93-
console.log('success!')
94-
onSuccess()
95-
}else{
96-
console.log('NOT SUCCESS?')
97-
}
98-
}
99-
}
100-
}
101-
}
102-
}catch(err){
103-
if(shouldExitEarly(processId)){
104-
// exit early
105-
return
106-
}
107-
// error contains output & error message
108-
// output can be parsed as json
109-
const{stdout, stderr}=err
110-
console.log('TEST FAILED',stdout)
111-
112-
if(!stdout){
113-
console.error('SOMETHING WENT WRONG WITH A PASSING TEST')
114-
onError()
115-
return
116-
}
117-
// test runner failed
118-
channel=getOutputChannel(outputChannelName)
119-
120-
if(stdout){
121-
constlines=stdout.split(/\r{0,1}\n/)
122-
123-
for(constlineoflines){
124-
if(line.length>0){
125-
constdataRegExp=/^{\"numFailedTestSuites"/
126-
constmatches=dataRegExp.exec(line)
127-
128-
if(matches&&matches.length){
129-
constresult=JSON.parse(line)
130-
constfirstError=result.testResults.find((t:any)=>t.status==='failed')
131-
132-
if(firstError){
133-
if(shouldExitEarly(processId)){
134-
// exit early
135-
return
136-
}
137-
onFail()
138-
}else{
139-
console.error('NOTE: PARSER DID NOT WORK FOR ',line)
140-
}
141-
}
142-
}
143-
}
144-
}
145-
146-
if(stderr){
147-
channel.show(false)
148-
channel.appendLine(stderr)
149-
}
150-
}
151-
}
152-
153-
exportdefaultrunTest

‎src/actions/runTest/testRunner.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
importnodefrom'../../services/node'
2+
import{getOutputChannel}from'./channel'
3+
import{setLatestProcess,isLatestProcess}from'./throttle'
4+
5+
// TODO: use tap parser to make it easier to support other test runners
6+
// TODO: how to load test runner parser
7+
// TODO: where to instantiate test runner
8+
9+
10+
interfaceCallbacks{
11+
onSuccess():void
12+
onFail():void
13+
onRun():void
14+
onError():void
15+
}
16+
17+
interfaceTestRunnerConfig{
18+
command:string
19+
parser(output:string):Error|null
20+
}
21+
22+
exportconstcreateTestRunner=(config:TestRunnerConfig,callbacks:Callbacks)=>{
23+
24+
constoutputChannelName='TEST_OUTPUT'
25+
26+
returnasync()=>{
27+
console.log('------------------- run test ------------------')
28+
29+
// track processId to prevent multiple
30+
constprocessId=setLatestProcess()
31+
if(!isLatestProcess(processId)){return}
32+
33+
// flag as running
34+
callbacks.onRun()
35+
36+
letresult:{stdout:string|undefined,stderr:string|undefined}
37+
try{
38+
result=awaitnode.exec(config.command)
39+
}catch(err){
40+
result=err
41+
}
42+
const{stdout, stderr}=result
43+
44+
// simple way to throttle requests
45+
if(!stdout||!isLatestProcess(processId)){return}
46+
47+
if(stderr){
48+
callbacks.onError()
49+
50+
// open terminal with error string
51+
constchannel=getOutputChannel(outputChannelName)
52+
channel.show(false)
53+
channel.appendLine(stderr)
54+
return
55+
}
56+
57+
// pass or fail?
58+
consttestsFailed=config.parser(stdout)
59+
if(testsFailed===null){
60+
callbacks.onSuccess()
61+
}else{
62+
// open terminal with failed test string
63+
constchannel=getOutputChannel(outputChannelName)
64+
channel.show(false)
65+
channel.appendLine(testsFailed.message)
66+
callbacks.onFail()
67+
}
68+
}
69+
}

‎src/actions/runTest/throttle.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// ensure only latest run_test action is taken
2+
letcurrentId=0
3+
4+
exportconstsetLatestProcess=()=>currentId++
5+
6+
// quick solution to prevent processing multiple results
7+
// NOTE: may be possible to kill child process early
8+
exportconstisLatestProcess=(processId:number):boolean=>currentId===processId

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp