/**
 * this module contains functions to show jest test results in
 * vscode inspector via the DiagnosticsCollection.
 */ import * as vscode from 'vscode' ; import { existsSync } from 'fs' ; import { TestFileAssertionStatus } from 'jest-editor-support' ; import { TestReconciliationState , TestResult } from './TestResults' ; import { testIdString } from './helpers' ; function createDiagnosticWithRange ( message , range , testName ? ) { const msg = testName ? `${ testName }\n-----\n${ message }` : message ; const diag = new vscode . Diagnostic ( range , msg , vscode . DiagnosticSeverity . Error ) ; diag . source = 'Jest' ; return diag ; } function createDiagnostic ( message , lineNumber , name ? , startCol = 0 , endCol = Number . MAX_SAFE_INTEGER ) { const line = lineNumber > 0 ? lineNumber - 1 : 0 ; return createDiagnosticWithRange ( message , new vscode . Range ( line , startCol , line , endCol ) , name ) ; } export function updateCurrentDiagnostics ( testResults , collection , editor ) { const uri = editor . document . uri ; if ( ! testResults . length ) { collection . delete ( uri ) ; return ; } const allDiagnostics = testResults . reduce ( ( list , tr ) => { const allResults = tr . multiResults ? [ tr , ... tr . multiResults ] : [ tr ] ; const diagnostics = allResults . filter ( ( r ) => r . status === TestReconciliationState . KnownFail ) . map ( ( r ) => { const line = r . lineNumberOfError || r . end . line ; const textLine = editor . document . lineAt ( line ) ; const name = testIdString ( 'display' , r . identifier ) ; return createDiagnosticWithRange ( r . shortMessage || r . terseMessage || 'unknown errorr' , textLine . range , name ) ; } ) ; list . push ( ... diagnostics ) ; return list ; } , [ ] as vscode . Diagnostic [ ] ) ; collection . set ( uri , allDiagnostics ) ; } export function updateDiagnostics ( testResults , collection ) { function addTestFileError ( result , uri ) { const diag = createDiagnostic ( result . message || 'test file error' , 0 , undefined , 0 , 0 ) ; collection . set ( uri , [ diag ] ) ; } function addTestsError ( result , uri ) { if ( ! result . assertions ) { return ; } const asserts = result . assertions . filter ( ( a ) => a . status === TestReconciliationState . KnownFail ) ; collection . set ( uri , asserts . map ( ( assertion ) => { const name = testIdString ( 'display' , assertion ) ; return createDiagnostic ( assertion . shortMessage || assertion . message , assertion . line ?? - 1 , name ) ; } ) ) ; } testResults . forEach ( ( result ) => { const uri = vscode . Uri . file ( result . file ) ; switch ( result . status ) { case TestReconciliationState . KnownFail : if ( result . assertions && result . assertions . length <= 0 ) { addTestFileError ( result , uri ) ; } else { addTestsError ( result , uri ) ; } break ; default : collection . delete ( uri ) ; break ; } } ) ; const toBeDeleted = [ ] ; collection . forEach ( ( uri ) => { if ( ! existsSync ( uri . fsPath ) ) { toBeDeleted . push ( uri ) ; } } ) ; toBeDeleted . forEach ( ( uri ) => { collection . delete ( uri ) ; } ) ; } export function resetDiagnostics ( diagnostics ) { diagnostics . clear ( ) ; } export function failedSuiteCount ( diagnostics ) { let sum = 0 ; diagnostics . forEach ( ( ) => sum ++ ) ; return sum ; }