import * as path from 'path' ; import * as vscode from 'vscode' ; import { lwcTestIndexer } from '../testIndexer' ; import { TestCaseInfo , TestFileInfo } from '../types' ; import { SfdxTestGroupNode , SfdxTestNode , sortTestNodeByLabel , TestNode } from './testNode' ; function getLabelFromTestCaseInfo ( testCaseInfo ) { const { testName } = testCaseInfo ; return testName ; } function getLabelFromTestFileInfo ( testFileInfo ) { const { testUri } = testFileInfo ; const { fsPath } = testUri ; const ext = '.test.js' ; const testGroupLabel = path . basename ( fsPath , ext ) ; return testGroupLabel ; } /**
 * Test Explorer Tree Data Provider implementation
 */ export class SfdxTestOutlineProvider implements vscode . TreeDataProvider < TestNode > , vscode . Disposable { private onDidChangeTestData : vscode . EventEmitter < TestNode | undefined > = new vscode . EventEmitter < TestNode | undefined > ( ) ; public onDidChangeTreeData = this . onDidChangeTestData . event ; private disposables : vscode . Disposable [ ] ; constructor ( ) { this . disposables = [ ] ; lwcTestIndexer . onDidUpdateTestIndex ( ( ) => { this . onDidUpdateTestIndex ( ) ; } , null , this . disposables ) ; lwcTestIndexer . onDidUpdateTestResultsIndex ( ( ) => { this . onDidUpdateTestResultsIndex ( ) ; } , null , this . disposables ) ; } public dispose ( ) { while ( this . disposables . length ) { const disposable = this . disposables . pop ( ) ; if ( disposable ) { disposable . dispose ( ) ; } } } private onDidUpdateTestIndex ( ) { this . onDidChangeTestData . fire ( undefined ) ; } private onDidUpdateTestResultsIndex ( ) { this . onDidChangeTestData . fire ( undefined ) ; } public getTreeItem ( element ) { return element ; } param element public async getChildren ( element ? ) { if ( element ) { if ( element instanceof SfdxTestGroupNode ) { if ( element . location ) { const testInfo = await lwcTestIndexer . findTestInfoFromLwcJestTestFile ( element . location . uri ) ; if ( testInfo ) { return testInfo . map ( testCaseInfo => { const testNodeLabel = getLabelFromTestCaseInfo ( testCaseInfo ) ; return new SfdxTestNode ( testNodeLabel , testCaseInfo ) ; } ) ; } } } return [ ] ; } else { try { const allTestFileInfo = await lwcTestIndexer . findAllTestFileInfo ( ) ; return allTestFileInfo . map ( testFileInfo => { const testNodeLabel = getLabelFromTestFileInfo ( testFileInfo ) ; const testGroupNode = new SfdxTestGroupNode ( testNodeLabel , testFileInfo ) ; return testGroupNode ; } ) . sort ( sortTestNodeByLabel ) ; } catch ( error ) { console . error ( error ) ; return [ ] ; } } } } param context export function registerLwcTestExplorerTreeView ( context ) { const testOutlineProvider = new SfdxTestOutlineProvider ( ) ; const testProvider = vscode . window . registerTreeDataProvider ( 'sfdx.force.lightning.lwc.test.view' , testOutlineProvider ) ; context . subscriptions . push ( testOutlineProvider ) ; context . subscriptions . push ( testProvider ) ; }