import * as vscode from 'vscode' ; import { DEFAULT_JEST_PATH , DEFAULT_JEST_DEBUG_PATH_WINDOWS , TERMINAL_NAME , } from './constants' ; import { getConfig , ConfigOption } from './config' ; import { quoteTestName , getTerminal , quoteArgument } from './extension' ; const convertEnvVariablesToObj = ( env ) => { const obj = ( env . split ( ' ' ) as string [ ] ) . filter ( ( v ) => ! ! v ) . reduce ( ( acc , v ) => { const [ key , val ] = v . split ( '=' ) ; acc [ key ] = val ; return acc ; } , { } as { [ key ] : string ; } ) ; return obj ; } ; export const runTest = ( filePath , testName ? , updateSnapshots = false ) => { const jestPath = getConfig ( ConfigOption . JestPath ) || DEFAULT_JEST_PATH ; const jestConfigPath = getConfig ( ConfigOption . JestConfigPath ) ; const runOptions = getConfig ( ConfigOption . JestCLIOptions ) as string [ ] ; const environmentVarialbes = getConfig ( ConfigOption . EnvironmentVariables ) as string ; let command = `${ environmentVarialbes } ${ jestPath } ${ quoteTestName ( filePath ) }` ; if ( testName ) { command += ` -t ${ quoteTestName ( testName ) }` ; } if ( jestConfigPath ) { command += ` -c ${ jestConfigPath }` ; } if ( updateSnapshots ) { command += ' -u' ; } if ( runOptions ) { runOptions . forEach ( ( option ) => { command += ` ${ option }` ; } ) ; } let terminal = getTerminal ( TERMINAL_NAME ) ; if ( ! terminal ) { terminal = vscode . window . createTerminal ( TERMINAL_NAME ) ; } terminal . show ( ) ; terminal . sendText ( command . trim ( ) ) ; } ; export const debugTest = ( filePath , testName ? ) => { const editor = vscode . window . activeTextEditor ; const jestPath = getConfig ( ConfigOption . JestPath ) || ( process . platform === 'win32' ? DEFAULT_JEST_DEBUG_PATH_WINDOWS : DEFAULT_JEST_PATH ) ; const jestConfigPath = getConfig ( ConfigOption . JestConfigPath ) ; const jestCLIOptions = getConfig ( ConfigOption . JestCLIOptions ) as string [ ] ; const environmentVarialbes = getConfig ( ConfigOption . EnvironmentVariables ) as string ; const args = [ filePath ] ; if ( testName ) { args . push ( '-t' , quoteTestName ( testName , 'none' ) ) ; } if ( jestConfigPath ) { args . push ( '-c' , jestConfigPath as string ) ; } if ( jestCLIOptions ) { jestCLIOptions . forEach ( ( option ) => { args . push ( option ) ; } ) ; } args . push ( '--runInBand' ) ; const debugConfig = { console : 'integratedTerminal' , internalConsoleOptions : 'neverOpen' , name : 'JestRunIt' , program : '${workspaceFolder}/' + jestPath , request : 'launch' , type : 'node' , args , env : convertEnvVariablesToObj ( environmentVarialbes ) , } ; vscode . debug . startDebugging ( vscode . workspace . getWorkspaceFolder ( editor ! . document . uri ) , debugConfig ) ; } ;