@@ -13,33 +13,35 @@ import {
1313} from './helpers' ;
1414import { platform } from 'os' ;
1515import { PluginResourceSettings } from './Settings' ;
16+ import { DebugInfo } from './types' ;
1617
1718export const DEBUG_CONFIG_PLATFORMS = [ 'windows' , 'linux' , 'osx' ] ;
1819const testNamePatternRegex = / \$ \{ j e s t .t e s t N a m e P a t t e r n \} / g;
1920const testFileRegex = / \$ \{ j e s t .t e s t F i l e \} / g;
2021const 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;
2123
2224export type DebugConfigOptions = Partial <
2325 Pick < PluginResourceSettings , 'jestCommandLine' | 'rootPath' | 'nodeEnv' >
2426> ;
2527type PartialDebugConfig = Partial < vscode . DebugConfiguration > ;
2628export class DebugConfigurationProvider implements vscode . DebugConfigurationProvider {
27- private fileNameToRun = '' ;
28- private testToRun = '' ;
29+ private debugInfo : DebugInfo | undefined ;
2930 private fromWorkspaceFolder : vscode . WorkspaceFolder | undefined ;
31+ private useJest30 : boolean | undefined ;
3032
3133 /**
3234 * Prepares injecting the name of the test, which has to be debugged, into the `DebugConfiguration`,
3335 * This function has to be called before `vscode.debug.startDebugging`.
3436 */
3537 public prepareTestRun (
36- fileNameToRun : string ,
37- testToRun : string ,
38- workspaceFolder : vscode . WorkspaceFolder
38+ debugInfo : DebugInfo ,
39+ workspaceFolder : vscode . WorkspaceFolder ,
40+ useJest30 ?: boolean
3941 ) : void {
40- this . fileNameToRun = fileNameToRun ;
41- this . testToRun = testToRun ;
42+ this . debugInfo = { ...debugInfo } ;
4243 this . fromWorkspaceFolder = workspaceFolder ;
44+ this . useJest30 = useJest30 ;
4345 }
4446
4547 getDebugConfigNames ( workspaceFolder ?: vscode . WorkspaceFolder ) : {
@@ -83,45 +85,69 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
8385
8486 const args = debugConfiguration . args || [ ] ;
8587
86- if ( this . fileNameToRun ) {
87- if ( this . testToRun ) {
88+ if ( this . debugInfo ) {
89+ if ( this . debugInfo . testName ) {
8890 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 ) ) ;
9099 }
91- args . push ( '--runTestsByPath' ) ;
92- args . push ( toFilePath ( this . fileNameToRun ) ) ;
93100
94- this . fileNameToRun = '' ;
95- this . testToRun = '' ;
101+ this . debugInfo = undefined ;
96102 }
97103
98104 debugConfiguration . args = args ;
99105 return debugConfiguration ;
100106 }
101107
108+ private getTestPathPatternOption ( ) : string {
109+ return this . useJest30 ? '--testPathPatterns' : '--testPathPattern' ;
110+ }
102111 /**
103112 * resolve v2 debug config
104113 * @param debugConfiguration v2 debug config
105114 * @returns
106115 */
107116 resolveDebugConfig2 ( debugConfiguration : vscode . DebugConfiguration ) : vscode . DebugConfiguration {
108117 if (
118+ ! this . debugInfo ||
109119 ! debugConfiguration . args ||
110120 ! Array . isArray ( debugConfiguration . args ) ||
111121 debugConfiguration . args . length <= 0
112122 ) {
113123 return debugConfiguration ;
114124 }
125+
126+ const debugInfo = this . debugInfo ;
115127 const args = debugConfiguration . args . map ( ( arg ) => {
116128 if ( typeof arg !== 'string' ) {
117129 return arg ;
118130 }
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+ }
119141 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+ ) ;
123148 } ) ;
124149 debugConfiguration . args = args ;
150+ this . debugInfo = undefined ;
125151
126152 return debugConfiguration ;
127153 }
0 commit comments