@@ -97,6 +97,23 @@ const valid = [
97
97
`
98
98
const element = getByText('value')
99
99
expect(element).toBeInTheDocument` ,
100
+
101
+ // *AllBy queries with `.toHaveLength(1)` is valid
102
+ // see conclusion at https://github.com/testing-library/eslint-plugin-jest-dom/issues/171#issuecomment-895074086
103
+ `expect(screen.getAllByRole('foo')).toHaveLength(1)` ,
104
+ `expect(await screen.findAllByRole('foo')).toHaveLength(1)` ,
105
+ `expect(getAllByRole('foo')).toHaveLength(1)` ,
106
+ `expect(wrapper.getAllByRole('foo')).toHaveLength(1)` ,
107
+ `const foo = screen.getAllByRole('foo');
108
+ expect(foo).toHaveLength(1);` ,
109
+ `const foo = getAllByRole('foo');
110
+ expect(foo).toHaveLength(1);` ,
111
+ `let foo;
112
+ foo = getAllByRole('foo');
113
+ expect(foo).toHaveLength(1);` ,
114
+ `let foo;
115
+ foo = screen.getAllByRole('foo');
116
+ expect(foo).toHaveLength(1);` ,
100
117
] ;
101
118
const invalid = [
102
119
invalidCase (
@@ -150,51 +167,6 @@ const invalid = [
150
167
foo = screen.getByText('foo');
151
168
expect(foo).toBeInTheDocument();`
152
169
) ,
153
- invalidCase (
154
- `expect(screen.getAllByRole('foo')).toHaveLength(1)` ,
155
- `expect(screen.getByRole('foo')).toBeInTheDocument()`
156
- ) ,
157
- invalidCase (
158
- `expect(await screen.findAllByRole('foo')).toHaveLength(1)` ,
159
- `expect(await screen.findByRole('foo')).toBeInTheDocument()`
160
- ) ,
161
- invalidCase (
162
- `expect(getAllByRole('foo')).toHaveLength(1)` ,
163
- `expect(getByRole('foo')).toBeInTheDocument()`
164
- ) ,
165
- invalidCase (
166
- `expect(wrapper.getAllByRole('foo')).toHaveLength(1)` ,
167
- `expect(wrapper.getByRole('foo')).toBeInTheDocument()`
168
- ) ,
169
- invalidCase (
170
- `const foo = screen.getAllByRole('foo');
171
- expect(foo).toHaveLength(1);` ,
172
- `const foo = screen.getByRole('foo');
173
- expect(foo).toBeInTheDocument();`
174
- ) ,
175
- invalidCase (
176
- `const foo = getAllByRole('foo');
177
- expect(foo).toHaveLength(1);` ,
178
- `const foo = getByRole('foo');
179
- expect(foo).toBeInTheDocument();`
180
- ) ,
181
- invalidCase (
182
- `let foo;
183
- foo = getAllByRole('foo');
184
- expect(foo).toHaveLength(1);` ,
185
- `let foo;
186
- foo = getByRole('foo');
187
- expect(foo).toBeInTheDocument();`
188
- ) ,
189
- invalidCase (
190
- `let foo;
191
- foo = screen.getAllByRole('foo');
192
- expect(foo).toHaveLength(1);` ,
193
- `let foo;
194
- foo = screen.getByRole('foo');
195
- expect(foo).toBeInTheDocument();`
196
- ) ,
197
-
198
170
invalidCase (
199
171
`expect(screen.getByText('foo')).toHaveLength(1)` ,
200
172
`expect(screen.getByText('foo')).toBeInTheDocument()`
@@ -461,12 +433,6 @@ const invalid = [
461
433
span = getByText('foo') as HTMLSpanElement
462
434
expect(span).toBeInTheDocument()`
463
435
) ,
464
- invalidCase (
465
- `const things = screen.getAllByText("foo");
466
- expect(things).toHaveLength(1);` ,
467
- `const things = screen.getByText("foo");
468
- expect(things).toBeInTheDocument();`
469
- ) ,
470
436
] ;
471
437
472
438
const ruleTester = new RuleTester ( {
0 commit comments