@@ -13,33 +13,35 @@ import {
13
13
} from './helpers' ;
14
14
import { platform } from 'os' ;
15
15
import { PluginResourceSettings } from './Settings' ;
16
+ import { DebugInfo } from './types' ;
16
17
17
18
export const DEBUG_CONFIG_PLATFORMS = [ 'windows' , 'linux' , 'osx' ] ;
18
19
const testNamePatternRegex = / \$ \{ j e s t .t e s t N a m e P a t t e r n \} / g;
19
20
const testFileRegex = / \$ \{ j e s t .t e s t F i l e \} / g;
20
21
const testFilePatternRegex = / \$ \{ j e s t .t e s t F i l e P a t t e r n \} / g;
22
+ const replaceTestPathPatternRegex = / - - ( t e s t P a t h P a t t e r n ( s ? ) | r u n T e s t s B y P a t h ) / g;
21
23
22
24
export type DebugConfigOptions = Partial <
23
25
Pick < PluginResourceSettings , 'jestCommandLine' | 'rootPath' | 'nodeEnv' >
24
26
> ;
25
27
type PartialDebugConfig = Partial < vscode . DebugConfiguration > ;
26
28
export class DebugConfigurationProvider implements vscode . DebugConfigurationProvider {
27
- private fileNameToRun = '' ;
28
- private testToRun = '' ;
29
+ private debugInfo : DebugInfo | undefined ;
29
30
private fromWorkspaceFolder : vscode . WorkspaceFolder | undefined ;
31
+ private useJest30 : boolean | undefined ;
30
32
31
33
/**
32
34
* Prepares injecting the name of the test, which has to be debugged, into the `DebugConfiguration`,
33
35
* This function has to be called before `vscode.debug.startDebugging`.
34
36
*/
35
37
public prepareTestRun (
36
- fileNameToRun : string ,
37
- testToRun : string ,
38
- workspaceFolder : vscode . WorkspaceFolder
38
+ debugInfo : DebugInfo ,
39
+ workspaceFolder : vscode . WorkspaceFolder ,
40
+ useJest30 ?: boolean
39
41
) : void {
40
- this . fileNameToRun = fileNameToRun ;
41
- this . testToRun = testToRun ;
42
+ this . debugInfo = { ...debugInfo } ;
42
43
this . fromWorkspaceFolder = workspaceFolder ;
44
+ this . useJest30 = useJest30 ;
43
45
}
44
46
45
47
getDebugConfigNames ( workspaceFolder ?: vscode . WorkspaceFolder ) : {
@@ -83,45 +85,69 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
83
85
84
86
const args = debugConfiguration . args || [ ] ;
85
87
86
- if ( this . fileNameToRun ) {
87
- if ( this . testToRun ) {
88
+ if ( this . debugInfo ) {
89
+ if ( this . debugInfo . testName ) {
88
90
args . push ( '--testNamePattern' ) ;
89
- args . push ( this . testToRun ) ;
91
+ args . push ( escapeRegExp ( this . debugInfo . testName ) ) ;
92
+ }
93
+ if ( this . debugInfo . useTestPathPattern ) {
94
+ args . push ( this . getTestPathPatternOption ( ) ) ;
95
+ args . push ( escapeRegExp ( this . debugInfo . testPath ) ) ;
96
+ } else {
97
+ args . push ( '--runTestsByPath' ) ;
98
+ args . push ( toFilePath ( this . debugInfo . testPath ) ) ;
90
99
}
91
- args . push ( '--runTestsByPath' ) ;
92
- args . push ( toFilePath ( this . fileNameToRun ) ) ;
93
100
94
- this . fileNameToRun = '' ;
95
- this . testToRun = '' ;
101
+ this . debugInfo = undefined ;
96
102
}
97
103
98
104
debugConfiguration . args = args ;
99
105
return debugConfiguration ;
100
106
}
101
107
108
+ private getTestPathPatternOption ( ) : string {
109
+ return this . useJest30 ? '--testPathPatterns' : '--testPathPattern' ;
110
+ }
102
111
/**
103
112
* resolve v2 debug config
104
113
* @param debugConfiguration v2 debug config
105
114
* @returns
106
115
*/
107
116
resolveDebugConfig2 ( debugConfiguration : vscode . DebugConfiguration ) : vscode . DebugConfiguration {
108
117
if (
118
+ ! this . debugInfo ||
109
119
! debugConfiguration . args ||
110
120
! Array . isArray ( debugConfiguration . args ) ||
111
121
debugConfiguration . args . length <= 0
112
122
) {
113
123
return debugConfiguration ;
114
124
}
125
+
126
+ const debugInfo = this . debugInfo ;
115
127
const args = debugConfiguration . args . map ( ( arg ) => {
116
128
if ( typeof arg !== 'string' ) {
117
129
return arg ;
118
130
}
131
+ if ( debugInfo . useTestPathPattern ) {
132
+ // if the debugInfo indicated this is a testPathPattern (such as running all tests within a folder)
133
+ // , we need to replace the --runTestsByPath argument with the correct --testPathPattern(s) argument
134
+ if ( replaceTestPathPatternRegex . test ( arg ) ) {
135
+ return arg . replace ( replaceTestPathPatternRegex , this . getTestPathPatternOption ( ) ) ;
136
+ }
137
+ if ( testFileRegex . test ( arg ) ) {
138
+ return arg . replace ( testFileRegex , escapeRegExp ( debugInfo . testPath ) ) ;
139
+ }
140
+ }
119
141
return arg
120
- . replace ( testFileRegex , toFilePath ( this . fileNameToRun ) )
121
- . replace ( testFilePatternRegex , escapeRegExp ( this . fileNameToRun ) )
122
- . replace ( testNamePatternRegex , escapeQuotes ( this . testToRun ) ) ;
142
+ . replace ( testFileRegex , toFilePath ( debugInfo . testPath ) )
143
+ . replace ( testFilePatternRegex , escapeRegExp ( debugInfo . testPath ) )
144
+ . replace (
145
+ testNamePatternRegex ,
146
+ debugInfo . testName ? escapeQuotes ( escapeRegExp ( debugInfo . testName ) ) : '.*'
147
+ ) ;
123
148
} ) ;
124
149
debugConfiguration . args = args ;
150
+ this . debugInfo = undefined ;
125
151
126
152
return debugConfiguration ;
127
153
}
0 commit comments