-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathDebugConfigurationProvider.ts
More file actions
79 lines (68 loc) · 2.8 KB
/
DebugConfigurationProvider.ts
File metadata and controls
79 lines (68 loc) · 2.8 KB
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
import * as vscode from 'vscode'
import { getTestCommand, isCreateReactAppTestCommand } from './helpers'
export class DebugConfigurationProvider implements vscode.DebugConfigurationProvider {
private fileNameToRun: string = ''
private testToRun: string = ''
/**
* Prepares injecting the name of the test, which has to be debugged, into the `DebugConfiguration`,
* This function has to be called before `vscode.debug.startDebugging`.
*/
public prepareTestRun(fileNameToRun: string, testToRun: string) {
this.fileNameToRun = fileNameToRun
this.testToRun = testToRun
}
resolveDebugConfiguration(
_folder: vscode.WorkspaceFolder | undefined,
debugConfiguration: vscode.DebugConfiguration,
_token?: vscode.CancellationToken
) {
if (debugConfiguration.name !== 'vscode-jest-tests') {
return debugConfiguration
}
if (!debugConfiguration.env) {
debugConfiguration.env = {}
}
// necessary for running CRA test scripts in non-watch mode
debugConfiguration.env.CI = 'vscode-jest-tests'
if (!debugConfiguration.args) {
debugConfiguration.args = []
}
if (this.fileNameToRun) {
debugConfiguration.args.push(this.fileNameToRun)
if (this.testToRun) {
debugConfiguration.args.push('--testNamePattern')
debugConfiguration.args.push(this.testToRun)
}
this.fileNameToRun = ''
this.testToRun = ''
}
return debugConfiguration
}
provideDebugConfigurations(folder: vscode.WorkspaceFolder | undefined, _token?: vscode.CancellationToken) {
// default jest config according to:
// https://github.com/Microsoft/vscode-recipes/tree/master/debugging-jest-tests#configure-launchjson-file-for-your-test-framework
// create-react-app config according to:
// https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#debugging-tests-in-visual-studio-code
const debugConfiguration: vscode.DebugConfiguration = {
type: 'node',
name: 'vscode-jest-tests',
request: 'launch',
args: ['--runInBand'],
cwd: '${workspaceFolder}',
console: 'integratedTerminal',
internalConsoleOptions: 'neverOpen',
}
const testCommand = folder && getTestCommand(folder.uri.fsPath)
if (isCreateReactAppTestCommand(testCommand)) {
const craCommand = testCommand.split(' ')
// Settings specific for projects bootstrapped with `create-react-app`
debugConfiguration.runtimeExecutable = '${workspaceFolder}/node_modules/.bin/' + craCommand.shift()
debugConfiguration.args = [...craCommand, ...debugConfiguration.args]
debugConfiguration.protocol = 'inspector'
} else {
// Plain jest setup
debugConfiguration.program = '${workspaceFolder}/node_modules/jest/bin/jest'
}
return [debugConfiguration]
}
}