forked from jest-community/vscode-jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JestProcess.ts
92 lines (80 loc) · 2.47 KB
/
JestProcess.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { platform } from 'os'
import { Runner, ProjectWorkspace } from 'jest-editor-support'
export class JestProcess {
static readonly keepAliveLimit = 5
private runner: Runner
private projectWorkspace: ProjectWorkspace
private onExitCallback: Function
private jestSupportEvents: Map<string, (...args: any[]) => void>
private resolve: Function
private keepAliveCounter: number
public keepAlive: boolean
public watchMode: boolean
public stopRequested: boolean
private startRunner() {
this.stopRequested = false
let exited = false
// Use a shell to run Jest command on Windows in order to correctly spawn `.cmd` files
// For details see https://github.com/jest-community/vscode-jest/issues/98
const useShell = platform() === 'win32'
this.runner = new Runner(this.projectWorkspace, { shell: useShell })
this.restoreJestEvents()
// pass watchMode into watchAll parameter also
this.runner.start(this.watchMode, this.watchMode)
this.runner.on('debuggerProcessExit', () => {
if (!exited) {
exited = true
if (--this.keepAliveCounter > 0) {
this.runner.removeAllListeners()
this.startRunner()
} else if (this.onExitCallback) {
this.onExitCallback(this)
if (this.stopRequested) {
this.resolve()
}
}
}
})
}
private restoreJestEvents() {
for (const [event, callback] of this.jestSupportEvents.entries()) {
this.runner.on(event, callback)
}
}
constructor({
projectWorkspace,
watchMode = false,
keepAlive = false,
}: {
projectWorkspace: ProjectWorkspace
watchMode?: boolean
keepAlive?: boolean
}) {
this.keepAlive = keepAlive
this.watchMode = watchMode
this.projectWorkspace = projectWorkspace
this.keepAliveCounter = keepAlive ? JestProcess.keepAliveLimit : 1
this.jestSupportEvents = new Map()
this.startRunner()
}
public onExit(callback: Function) {
this.onExitCallback = callback
}
public onJestEditorSupportEvent(event, callback) {
this.jestSupportEvents.set(event, callback)
this.runner.on(event, callback)
return this
}
public stop() {
this.stopRequested = true
this.keepAliveCounter = 1
this.jestSupportEvents.clear()
this.runner.closeProcess()
return new Promise(resolve => {
this.resolve = resolve
})
}
public runJestWithUpdateForSnapshots(callback) {
this.runner.runJestWithUpdateForSnapshots(callback)
}
}