1
1
import { 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' ;
3
8
4
9
import type { TSESTree } from '@typescript-eslint/utils' ;
5
10
@@ -44,6 +49,24 @@ export default createTestingLibraryRule<Options, MessageIds>({
44
49
} ) as Array < TSESTree . ExpressionStatement > ;
45
50
}
46
51
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
+
47
70
function reportMultipleAssertion ( node : TSESTree . BlockStatement ) {
48
71
if ( ! node . parent ) {
49
72
return ;
@@ -62,14 +85,30 @@ export default createTestingLibraryRule<Options, MessageIds>({
62
85
63
86
const expectNodes = getExpectNodes ( node . body ) ;
64
87
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 ) ;
67
103
}
68
104
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 ) ) {
71
110
context . report ( {
72
- node : expectNodes [ i ] ,
111
+ node : expressionStatement ,
73
112
messageId : 'noWaitForMultipleAssertion' ,
74
113
} ) ;
75
114
}
0 commit comments