11import { createTestingLibraryRule } from '../create-testing-library-rule' ;
2- import { getPropertyIdentifierNode } from '../node-utils' ;
2+ import {
3+ getPropertyIdentifierNode ,
4+ isCallExpression ,
5+ isMemberExpression ,
6+ } from '../node-utils' ;
7+ import { getSourceCode } from '../utils' ;
38
49import type { TSESTree } from '@typescript-eslint/utils' ;
510
@@ -44,6 +49,24 @@ export default createTestingLibraryRule<Options, MessageIds>({
4449 } ) as Array < TSESTree . ExpressionStatement > ;
4550 }
4651
52+ function getExpectArgument ( expression : TSESTree . Expression ) {
53+ if ( ! isCallExpression ( expression ) ) {
54+ return null ;
55+ }
56+
57+ const { callee } = expression ;
58+ if ( ! isMemberExpression ( callee ) ) {
59+ return null ;
60+ }
61+
62+ const { object } = callee ;
63+ if ( ! isCallExpression ( object ) || object . arguments . length === 0 ) {
64+ return null ;
65+ }
66+
67+ return object . arguments [ 0 ] ;
68+ }
69+
4770 function reportMultipleAssertion ( node : TSESTree . BlockStatement ) {
4871 if ( ! node . parent ) {
4972 return ;
@@ -62,14 +85,30 @@ export default createTestingLibraryRule<Options, MessageIds>({
6285
6386 const expectNodes = getExpectNodes ( node . body ) ;
6487
65- if ( expectNodes . length <= 1 ) {
66- return ;
88+ const expectArgumentMap = new Map <
89+ string ,
90+ TSESTree . ExpressionStatement [ ]
91+ > ( ) ;
92+
93+ for ( const expectNode of expectNodes ) {
94+ const argument = getExpectArgument ( expectNode . expression ) ;
95+ if ( ! argument ) {
96+ continue ;
97+ }
98+
99+ const argumentText = getSourceCode ( context ) . getText ( argument ) ;
100+ const existingNodes = expectArgumentMap . get ( argumentText ) ?? [ ] ;
101+ const newTargetNodes = [ ...existingNodes , expectNode ] ;
102+ expectArgumentMap . set ( argumentText , newTargetNodes ) ;
67103 }
68104
69- for ( let i = 0 ; i < expectNodes . length ; i ++ ) {
70- if ( i !== 0 ) {
105+ for ( const expressionStatements of expectArgumentMap . values ( ) ) {
106+ if ( expressionStatements . length <= 1 ) {
107+ continue ;
108+ }
109+ for ( const expressionStatement of expressionStatements . slice ( 1 ) ) {
71110 context . report ( {
72- node : expectNodes [ i ] ,
111+ node : expressionStatement ,
73112 messageId : 'noWaitForMultipleAssertion' ,
74113 } ) ;
75114 }
0 commit comments