Skip to content

Commit 3860f1e

Browse files
authored
fix(no-conditional-expect): Fix false positive with asymmetric matchers (#304)
* Expect parsing * Get it working * Fix up * Tests * Re-org
1 parent 92e9240 commit 3860f1e

File tree

5 files changed

+129
-118
lines changed

5 files changed

+129
-118
lines changed

src/rules/no-conditional-expect.test.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
import dedent from 'dedent'
21
import rule from '../../src/rules/no-conditional-expect'
3-
import { runRuleTester } from '../utils/rule-tester'
2+
import { javascript, runRuleTester } from '../utils/rule-tester'
43

54
const messageId = 'conditionalExpect'
65

76
runRuleTester('common tests', rule, {
87
invalid: [],
98
valid: [
10-
`
9+
javascript`
1110
test('foo', () => {
1211
expect(1).toBe(2);
1312
});
1413
`,
15-
`
14+
javascript`
1615
test('foo', () => {
1716
expect(!true).toBe(false);
1817
});
1918
`,
19+
{
20+
code: javascript`
21+
test('foo', () => {
22+
const expected = arr.map((x) => {
23+
if (typeof x === 'string') {
24+
return expect.arrayContaining([x])
25+
} else {
26+
return b
27+
}
28+
})
29+
30+
expect([1, 2, 3]).toEqual(expected);
31+
});
32+
`,
33+
},
2034
],
2135
})
2236

@@ -154,7 +168,7 @@ runRuleTester('logical conditions', rule, {
154168
`,
155169
// Global aliases
156170
{
157-
code: dedent`
171+
code: javascript`
158172
it('foo', () => {
159173
process.env.FAIL && setNum(1);
160174
@@ -331,7 +345,6 @@ runRuleTester('catch conditions', rule, {
331345
code: `
332346
test('foo', () => {
333347
try {
334-
335348
} catch (err) {
336349
expect(err).toMatch('Error');
337350
}
@@ -397,7 +410,7 @@ runRuleTester('catch conditions', rule, {
397410
runRuleTester('promises', rule, {
398411
invalid: [
399412
{
400-
code: dedent`
413+
code: javascript`
401414
test('works', async () => {
402415
await Promise.resolve()
403416
.then(() => { throw new Error('oh noes!'); })
@@ -407,7 +420,7 @@ runRuleTester('promises', rule, {
407420
errors: [{ messageId }],
408421
},
409422
{
410-
code: dedent`
423+
code: javascript`
411424
test('works', async () => {
412425
await Promise.resolve()
413426
.then(() => { throw new Error('oh noes!'); })
@@ -421,7 +434,7 @@ runRuleTester('promises', rule, {
421434
errors: [{ messageId }],
422435
},
423436
{
424-
code: dedent`
437+
code: javascript`
425438
test('works', async () => {
426439
await Promise.resolve()
427440
.catch(error => expect(error).toBeInstanceOf(Error))
@@ -432,7 +445,7 @@ runRuleTester('promises', rule, {
432445
errors: [{ messageId }],
433446
},
434447
{
435-
code: dedent`
448+
code: javascript`
436449
test('works', async () => {
437450
await Promise.resolve()
438451
.catch(error => expect(error).toBeInstanceOf(Error))
@@ -444,7 +457,7 @@ runRuleTester('promises', rule, {
444457
errors: [{ messageId }],
445458
},
446459
{
447-
code: dedent`
460+
code: javascript`
448461
test('works', async () => {
449462
await somePromise
450463
.then(() => { throw new Error('oh noes!'); })
@@ -454,7 +467,7 @@ runRuleTester('promises', rule, {
454467
errors: [{ messageId }],
455468
},
456469
{
457-
code: dedent`
470+
code: javascript`
458471
test('works', async () => {
459472
await somePromise.catch(error => expect(error).toBeInstanceOf(Error));
460473
});

src/rules/valid-expect.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ runRuleTester('valid-expect', rule, {
215215
valid: [
216216
{ code: 'expectPayButtonToBeEnabled()' },
217217
{ code: 'expect("something").toBe("else")' },
218+
{ code: 'expect.anything()' },
219+
{ code: 'expect.arrayContaining()' },
220+
{ code: 'expect.not.arrayContaining()' },
221+
{ code: 'expect.objectContaining(expected)' },
222+
{ code: 'expect.not.objectContaining(expected)' },
218223
{ code: 'expect("something").toBe(expect.anything())' },
219224
{ code: 'expect("something").toEqual({ foo: expect.anything() })' },
220225
{ code: 'expect("something").toBe(expect.arrayContaining([1, 2, 3]))' },

src/utils/parseFnCall.test.ts

Lines changed: 13 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -260,36 +260,6 @@ runRuleTester('expect', rule, {
260260
code: dedent`
261261
import { expect } from '@playwright/test';
262262
263-
expect.assertions();
264-
`,
265-
errors: [
266-
{
267-
column: 1,
268-
data: expectedParsedFnCallResultData({
269-
args: [],
270-
group: 'expect',
271-
head: {
272-
local: 'expect',
273-
node: 'expect',
274-
original: null,
275-
},
276-
matcher: 'assertions',
277-
matcherArgs: [],
278-
matcherName: 'assertions',
279-
members: ['assertions'],
280-
modifiers: [],
281-
name: 'expect',
282-
type: 'expect',
283-
}),
284-
line: 3,
285-
messageId: 'details',
286-
},
287-
],
288-
},
289-
{
290-
code: dedent`
291-
import { expect } from '@playwright/test';
292-
293263
expect(x).toBe(y);
294264
`,
295265
errors: [
@@ -380,58 +350,6 @@ runRuleTester('expect', rule, {
380350
code: dedent`
381351
import { expect } from '@playwright/test';
382352
383-
expect.anything();
384-
expect.not.arrayContaining();
385-
`,
386-
errors: [
387-
{
388-
column: 1,
389-
data: expectedParsedFnCallResultData({
390-
args: [],
391-
group: 'expect',
392-
head: {
393-
local: 'expect',
394-
node: 'expect',
395-
original: null,
396-
},
397-
matcher: 'anything',
398-
matcherArgs: [],
399-
matcherName: 'anything',
400-
members: ['anything'],
401-
modifiers: [],
402-
name: 'expect',
403-
type: 'expect',
404-
}),
405-
line: 3,
406-
messageId: 'details',
407-
},
408-
{
409-
column: 1,
410-
data: expectedParsedFnCallResultData({
411-
args: [],
412-
group: 'expect',
413-
head: {
414-
local: 'expect',
415-
node: 'expect',
416-
original: null,
417-
},
418-
matcher: 'arrayContaining',
419-
matcherArgs: [],
420-
matcherName: 'arrayContaining',
421-
members: ['not', 'arrayContaining'],
422-
modifiers: ['not'],
423-
name: 'expect',
424-
type: 'expect',
425-
}),
426-
line: 4,
427-
messageId: 'details',
428-
},
429-
],
430-
},
431-
{
432-
code: dedent`
433-
import { expect } from '@playwright/test';
434-
435353
expect;
436354
expect(x);
437355
expect(x).toBe;
@@ -1187,5 +1105,18 @@ runTSRuleTester('typescript', rule, {
11871105
},
11881106
"it('is not a function', () => {});",
11891107
'dedent()',
1108+
'expect.assertions()',
1109+
'expect.anything()',
1110+
'expect.arrayContaining()',
1111+
'expect.objectContaining(expected)',
1112+
'expect.not.objectContaining(expected)',
1113+
{
1114+
code: dedent`
1115+
import { expect } from '@playwright/test';
1116+
1117+
expect.assertions();
1118+
expect.anything();
1119+
`,
1120+
},
11901121
],
11911122
})

0 commit comments

Comments
 (0)