import * as vscode from 'vscode' ; import { TestExecutionInfo } from '../types' ; import { FORCE_LWC_TEST_WATCH_LOG_NAME } from '../types/constants' ; import { SFDX_LWC_JEST_IS_WATCHING_FOCUSED_FILE_CONTEXT } from '../types/constants' ; import { SfdxTask } from './taskService' ; import { TestRunner , TestRunType } from './testRunner' ; /**
 * Test Watcher class for watching Jest tests
 */ class TestWatcher { private watchedTests : Map < string , SfdxTask > = new Map ( ) ; param testExecutionInfo public async watchTest ( testExecutionInfo ) { const testRunner = new TestRunner ( testExecutionInfo , TestRunType . WATCH , FORCE_LWC_TEST_WATCH_LOG_NAME ) ; try { const sfdxTask = await testRunner . executeAsSfdxTask ( ) ; if ( sfdxTask ) { const { testUri } = testExecutionInfo ; const { fsPath } = testUri ; sfdxTask . onDidEnd ( ( ) => { this . watchedTests . delete ( fsPath ) ; this . setWatchingContext ( testUri ) ; } ) ; this . watchedTests . set ( fsPath , sfdxTask ) ; this . setWatchingContext ( testUri ) ; return sfdxTask ; } } catch ( error ) { console . error ( error ) ; } } param testExecutionInfo public stopWatchingTest ( testExecutionInfo ) { const { testUri } = testExecutionInfo ; const { fsPath } = testUri ; const watchTestTask = this . watchedTests . get ( fsPath ) ; if ( watchTestTask ) { watchTestTask . terminate ( ) ; } this . watchedTests . delete ( fsPath ) ; this . setWatchingContext ( testUri ) ; } /**
     * Stop watching all tests.
     */ public stopWatchingAllTests ( ) { for ( const [ fsPath , watchTestTask ] of this . watchedTests . entries ( ) ) { if ( watchTestTask ) { watchTestTask . terminate ( ) ; } this . watchedTests . delete ( fsPath ) ; this . setWatchingContext ( vscode . Uri . file ( fsPath ) ) ; } } param testUri public isWatchingTest ( testUri ) { const { fsPath } = testUri ; return this . watchedTests . has ( fsPath ) ; } param testUri public setWatchingContext ( testUri ) { if ( vscode . window . activeTextEditor && vscode . window . activeTextEditor . document . uri . fsPath === testUri . fsPath ) { vscode . commands . executeCommand ( 'setContext' , SFDX_LWC_JEST_IS_WATCHING_FOCUSED_FILE_CONTEXT , this . isWatchingTest ( testUri ) ) ; } } } export const testWatcher = new TestWatcher ( ) ;