@@ -263,31 +263,33 @@ export function transformExpectAsync(
263263 return node ;
264264 }
265265
266- // When the chain continues past this call (e.g. the `withContext('msg')` link itself),
267- // the outermost matcher call handles the whole statement.
268- if (
269- node . parent &&
270- ts . isPropertyAccessExpression ( node . parent ) &&
271- node . parent . expression === node
272- ) {
273- return node ;
274- }
275-
276266 const matcherCall = node ;
277267 const matcherPae = node . expression ;
268+ const matcherName = ts . isIdentifier ( matcherPae . name ) ? matcherPae . name . text : undefined ;
269+ if ( ! matcherName || matcherName === 'withContext' ) {
270+ return node ;
271+ }
278272
279273 // Walk down any modifier chain (`.not`, `.already`, `.withContext(...)`) to the root call.
280274 // Modifiers have no direct vitest equivalent, so a modified matcher falls through to the
281- // TODO handling below.
275+ // TODO handling below. `withContext` is the only chainable modifier method, so any other
276+ // intermediate call (e.g. a `.then(...)` chained on a matcher) is not an `expectAsync` chain.
282277 const modifierNames : string [ ] = [ ] ;
283278 let expectCall : ts . CallExpression | undefined ;
284279 let current : ts . Expression = matcherPae . expression ;
285280 while ( ! expectCall ) {
286281 if ( ts . isCallExpression ( current ) ) {
287- if ( ts . isIdentifier ( current . expression ) ) {
282+ if ( ts . isIdentifier ( current . expression ) && current . expression . text === 'expectAsync' ) {
288283 expectCall = current ;
284+ } else if (
285+ ts . isPropertyAccessExpression ( current . expression ) &&
286+ ts . isIdentifier ( current . expression . name ) &&
287+ current . expression . name . text === 'withContext'
288+ ) {
289+ modifierNames . unshift ( 'withContext' ) ;
290+ current = current . expression . expression ;
289291 } else {
290- current = current . expression ;
292+ return node ;
291293 }
292294 } else if ( ts . isPropertyAccessExpression ( current ) && ts . isIdentifier ( current . name ) ) {
293295 modifierNames . unshift ( current . name . text ) ;
@@ -297,13 +299,7 @@ export function transformExpectAsync(
297299 }
298300 }
299301
300- if ( ! ts . isIdentifier ( expectCall . expression ) || expectCall . expression . text !== 'expectAsync' ) {
301- return node ;
302- }
303-
304- const matcherName = ts . isIdentifier ( matcherPae . name ) ? matcherPae . name . text : undefined ;
305- const mapping =
306- matcherName && modifierNames . length === 0 ? ASYNC_MATCHER_CHANGES . get ( matcherName ) : undefined ;
302+ const mapping = modifierNames . length === 0 ? ASYNC_MATCHER_CHANGES . get ( matcherName ) : undefined ;
307303
308304 if ( mapping ) {
309305 reporter . reportTransformation (
@@ -324,16 +320,14 @@ export function transformExpectAsync(
324320 return ts . factory . createCallExpression ( newMatcherChain , undefined , newMatcherArgs ) ;
325321 }
326322
327- if ( matcherName ) {
328- if ( matcherName === 'toBePending' && modifierNames . length === 0 ) {
329- const category = 'toBePending' ;
330- reporter . recordTodo ( category , sourceFile , node ) ;
331- addTodoComment ( node , category ) ;
332- } else {
333- const category = 'unsupported-expect-async-matcher' ;
334- reporter . recordTodo ( category , sourceFile , node ) ;
335- addTodoComment ( node , category , { name : [ ...modifierNames , matcherName ] . join ( '.' ) } ) ;
336- }
323+ if ( matcherName === 'toBePending' && modifierNames . length === 0 ) {
324+ const category = 'toBePending' ;
325+ reporter . recordTodo ( category , sourceFile , node ) ;
326+ addTodoComment ( node , category ) ;
327+ } else {
328+ const category = 'unsupported-expect-async-matcher' ;
329+ reporter . recordTodo ( category , sourceFile , node ) ;
330+ addTodoComment ( node , category , { name : [ ...modifierNames , matcherName ] . join ( '.' ) } ) ;
337331 }
338332
339333 return node ;
0 commit comments