@@ -14,12 +14,11 @@ import {
14
14
TestAssertionStatus ,
15
15
ParsedNode ,
16
16
DescribeBlock ,
17
- Location ,
17
+ CodeLocation as Location ,
18
18
NamedBlock ,
19
- ParsedNodeTypes ,
19
+ ParsedNodeType ,
20
20
} from 'jest-editor-support' ;
21
- import { TestReconciliationState } from './TestReconciliationState' ;
22
- import { TestResult } from './TestResult' ;
21
+ import { TestResult , TestStatus , LocationRange } from './TestResult' ;
23
22
import {
24
23
DataNode ,
25
24
ContainerNode ,
@@ -32,6 +31,14 @@ import {
32
31
MatchOptions ,
33
32
} from './match-node' ;
34
33
34
+ interface MaybeWithLocation {
35
+ start ?: Location ;
36
+ end ?: Location ;
37
+ }
38
+
39
+ export const hasLocation = ( loc : MaybeWithLocation ) : loc is LocationRange =>
40
+ loc . start != null && loc . end != null ;
41
+
35
42
export const buildAssertionContainer = (
36
43
assertions : TestAssertionStatus [ ]
37
44
) : ContainerNode < TestAssertionStatus > => {
@@ -80,11 +87,12 @@ export const buildSourceContainer = (sourceRoot: ParsedNode): ContainerNode<ItBl
80
87
end : namedNode . end ? adjustLocation ( namedNode . end ) : UnknownRange . end ,
81
88
} ,
82
89
} ) ;
90
+ const start = ( node . start ?. line ?? 0 ) - 1 ;
83
91
if ( isDescribeBlock ( node ) ) {
84
- container = new ContainerNode ( node . name , node . start ?. line - 1 , attrs ( node ) ) ;
92
+ container = new ContainerNode ( node . name , start , attrs ( node ) ) ;
85
93
parent . addContainerNode ( container ) ;
86
94
} else if ( isItBlock ( node ) ) {
87
- parent . addDataNode ( new DataNode ( node . name , node . start . line - 1 , node , attrs ( node ) ) ) ;
95
+ parent . addDataNode ( new DataNode ( node . name , start , node , attrs ( node ) ) ) ;
88
96
}
89
97
90
98
node . children ?. forEach ( ( n ) => buildNode ( n , container ) ) ;
@@ -101,8 +109,14 @@ export const buildSourceContainer = (sourceRoot: ParsedNode): ContainerNode<ItBl
101
109
return root ;
102
110
} ;
103
111
104
- const adjustLocation = ( l : Location ) : Location => ( { column : l . column - 1 , line : l . line - 1 } ) ;
112
+ const adjustLocation = ( l ?: Location ) : Location => ( {
113
+ column : l ? l . column - 1 : 0 ,
114
+ line : l ? l . line - 1 : 0 ,
115
+ } ) ;
105
116
const matchPos = ( t : ItBlock , a : TestAssertionStatus , forError = false ) : boolean => {
117
+ if ( ! hasLocation ( t ) ) {
118
+ return false ;
119
+ }
106
120
const line = forError ? a . line : a . line ?? a . location ?. line ;
107
121
return ( line != null && line >= t . start . line && line <= t . end . line ) || false ;
108
122
} ;
@@ -125,7 +139,11 @@ export const toMatchResult = (
125
139
? [ undefined , undefined , assertionOrErr ]
126
140
: [ assertionOrErr . data , assertionOrErr . history ( reason ) , undefined ] ;
127
141
142
+ if ( ! test . start || ! test . end ) {
143
+ console . warn ( `missing location for test block: ${ test . name } ` ) ;
144
+ }
128
145
// Note the shift from one-based to zero-based line number and columns
146
+ // assumption: if we reached here, the test start/end must have been defined
129
147
return {
130
148
name : assertion ?. fullName ?? assertion ?. title ?? sourceName ,
131
149
identifier : {
@@ -134,11 +152,15 @@ export const toMatchResult = (
134
152
} ,
135
153
start : adjustLocation ( test . start ) ,
136
154
end : adjustLocation ( test . end ) ,
137
- status : assertion ?. status ?? TestReconciliationState . Unknown ,
155
+ status : assertion ?. status ?? TestStatus . Unknown ,
138
156
shortMessage : assertion ?. shortMessage ?? err ,
139
157
terseMessage : assertion ?. terseMessage ,
140
158
lineNumberOfError :
141
- assertion ?. line && matchPos ( test , assertion , true ) ? assertion . line - 1 : test . end . line - 1 ,
159
+ assertion ?. line && matchPos ( test , assertion , true )
160
+ ? assertion . line - 1
161
+ : test . end
162
+ ? test . end . line - 1
163
+ : 0 ,
142
164
sourceHistory,
143
165
assertionHistory,
144
166
} ;
@@ -458,8 +480,10 @@ const ContextMatch = (): ContextMatchAlgorithm => {
458
480
459
481
const { match } = ContextMatch ( ) ;
460
482
const isParsedNode = ( source : ParsedNode | ContainerNode < ItBlock > ) : source is ParsedNode =>
461
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
462
- ( source as any ) . type in ParsedNodeTypes ;
483
+ typeof source === 'object' &&
484
+ 'type' in source &&
485
+ Object . values ( ParsedNodeType ) . includes ( source . type ) ;
486
+
463
487
export const matchTestAssertions = (
464
488
fileName : string ,
465
489
source : ParsedNode | ContainerNode < ItBlock > ,
0 commit comments