import * as vscode from 'vscode' ; import { toFilePath , getTestCommand , isCreateReactAppTestCommand , escapeRegExp } from './helpers' ; const testNamePatternRegex = /\$\{jest.testNamePattern\}/g ; const testFileRegex = /\$\{jest.testFile\}/g ; const testFilePatternRegex = /\$\{jest.testFilePattern\}/g ; export class DebugConfigurationProvider implements vscode . DebugConfigurationProvider { private fileNameToRun = '' ; private testToRun = '' ; /**
     * 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 , testToRun ) { this . fileNameToRun = fileNameToRun ; this . testToRun = testToRun ; } resolveDebugConfiguration ( _folder , debugConfiguration , _token ? ) { if ( debugConfiguration . name === 'vscode-jest-tests.v2' ) { return this . resolveDebugConfig2 ( debugConfiguration ) ; } if ( debugConfiguration . name !== 'vscode-jest-tests' ) { return debugConfiguration ; } if ( ! debugConfiguration . env ) { debugConfiguration . env = { } ; } debugConfiguration . env . CI = 'vscode-jest-tests' ; const args = debugConfiguration . args || [ ] ; if ( this . fileNameToRun ) { if ( this . testToRun ) { args . push ( '--testNamePattern' ) ; args . push ( this . testToRun ) ; } args . push ( '--runTestsByPath' ) ; args . push ( toFilePath ( this . fileNameToRun ) ) ; this . fileNameToRun = '' ; this . testToRun = '' ; } debugConfiguration . args = args ; return debugConfiguration ; } param debugConfiguration returns resolveDebugConfig2 ( debugConfiguration ) { if ( ! debugConfiguration . args || ! Array . isArray ( debugConfiguration . args ) || debugConfiguration . args . length <= 0 ) { return debugConfiguration ; } const args = debugConfiguration . args . map ( ( arg ) => { if ( typeof arg !== 'string' ) { return arg ; } switch ( true ) { case testFileRegex . test ( arg ) : { return arg . replace ( testFileRegex , toFilePath ( this . fileNameToRun ) ) ; } case testFilePatternRegex . test ( arg ) : { return arg . replace ( testFilePatternRegex , escapeRegExp ( this . fileNameToRun ) ) ; } case testNamePatternRegex . test ( arg ) : { return arg . replace ( testNamePatternRegex , this . testToRun ) ; } default : return arg ; } } ) ; debugConfiguration . args = args ; return debugConfiguration ; } param folder param _token returns provideDebugConfigurations ( folder , _token ? ) { const debugConfiguration = { type : 'node' , name : 'vscode-jest-tests.v2' , request : 'launch' , args : [ '--runInBand' , '--watchAll=false' , '--testNamePattern' , '${jest.testNamePattern}' , '--runTestsByPath' , '${jest.testFile}' , ] , cwd : '${workspaceFolder}' , console : 'integratedTerminal' , internalConsoleOptions : 'neverOpen' , disableOptimisticBPs : true , } ; const testCommand = folder && getTestCommand ( folder . uri . fsPath ) ; if ( testCommand && isCreateReactAppTestCommand ( testCommand ) ) { const craCommand = testCommand . split ( ' ' ) ; debugConfiguration . runtimeExecutable = '${workspaceFolder}/node_modules/.bin/' + craCommand . shift ( ) ; debugConfiguration . args = [ ... craCommand , ... debugConfiguration . args ] ; debugConfiguration . protocol = 'inspector' ; } else { debugConfiguration . program = '${workspaceFolder}/node_modules/.bin/jest' ; debugConfiguration . windows = { program : '${workspaceFolder}/node_modules/jest/bin/jest' , } ; } return [ debugConfiguration ] ; } }