import { shared as lspCommon } from '@salesforce/lightning-lsp-common' ; import * as pathUtils from '@salesforce/salesforcedx-utils-vscode/out/src/helpers' ; import * as fs from 'fs' ; import * as path from 'path' ; import * as which from 'which' ; import { SinonStub , stub } from 'sinon' ; import * as vscode from 'vscode' ; import URI from 'vscode-uri' ; import { SfdxTask } from '../../../../src/testSupport/testRunner/taskService' ; import { testResultsWatcher } from '../../../../src/testSupport/testRunner/testResultsWatcher' ; import { TestFileInfo , TestInfoKind , TestType } from '../../../../src/testSupport/types' ; import { mockPreviewJavaScriptDebugger , unmockPreviewJavaScriptDebugger } from './vscodeConfiguration' ; export { mockPreviewJavaScriptDebugger , unmockPreviewJavaScriptDebugger } ; let existsSyncStub ; let whichSyncStub ; let sfdxTaskExecuteStub ; let activeTextEditorStub ; let getTempFolderStub ; let watchTestResultsStub ; export function createMockTestFileInfo ( mockTestFile = 'mockTest.test.js' ) { const mockDirectory = path . join ( vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath , 'force-app' , 'main' , 'lwc' , 'mockComponent' , '__tests__' ) ; const mockTestFilePath = path . join ( mockDirectory , mockTestFile ) ; const testExecutionInfo = { kind : TestInfoKind . TEST_FILE , testType : TestType . LWC , testUri : URI . file ( mockTestFilePath ) } ; return testExecutionInfo ; } export function mockGetLwcTestRunnerExecutable ( mockWorkspaceType = lspCommon . WorkspaceType . SFDX ) { if ( mockWorkspaceType === lspCommon . WorkspaceType . SFDX ) { existsSyncStub = stub ( fs , 'existsSync' ) ; existsSyncStub . returns ( true ) ; } if ( mockWorkspaceType === lspCommon . WorkspaceType . CORE_ALL || mockWorkspaceType === lspCommon . WorkspaceType . CORE_PARTIAL ) { whichSyncStub = stub ( which , 'sync' ) ; whichSyncStub . returns ( path . join ( '/bin' , 'lwc-test' ) ) ; existsSyncStub = stub ( fs , 'existsSync' ) ; existsSyncStub . returns ( true ) ; } } export function unmockGetLwcTestRunnerExecutable ( ) { if ( existsSyncStub ) { existsSyncStub . restore ( ) ; } if ( whichSyncStub ) { whichSyncStub . restore ( ) ; } } export function mockSfdxTaskExecute ( immediate ? ) { sfdxTaskExecuteStub = stub ( SfdxTask . prototype , 'execute' ) ; sfdxTaskExecuteStub . callsFake ( async function ( this ) : Promise < SfdxTask > { if ( immediate ) { this . notifyEndTask ( ) ; return this ; } const task = this ; return Promise . resolve ( ) . then ( ( ) => { setTimeout ( ( ) => { task . notifyEndTask ( ) ; } , 0 ) ; return task ; } ) ; } ) ; } export function unmockSfdxTaskExecute ( ) { sfdxTaskExecuteStub . restore ( ) ; } param testUri export function mockActiveTextEditorUri ( testUri ) { const mockActiveTextEditor = { document : { uri : testUri , languageId : 'javascript' } } ; activeTextEditorStub = stub ( vscode . window , 'activeTextEditor' ) . get ( ( ) => { return mockActiveTextEditor ; } ) ; } export function unmockActiveTextEditorUri ( ) { activeTextEditorStub . restore ( ) ; } /**
 * Mock test result watcher's get temp folder and watch test results methods
 */ export function mockTestResultWatcher ( ) { getTempFolderStub = stub ( pathUtils , 'getTestResultsFolder' ) ; getTempFolderStub . callsFake ( ( vscodePath , testType ) => { return path . join ( vscodePath , '.sfdx' , 'tools' , 'testresults' , testType ) ; } ) ; watchTestResultsStub = stub ( testResultsWatcher , 'watchTestResults' ) ; watchTestResultsStub . callsFake ( ( ) => { } ) ; } export function unmockTestResultWatcher ( ) { getTempFolderStub . restore ( ) ; watchTestResultsStub . restore ( ) ; }