Skip to content

Commit

Permalink
fix(no-conditional-expect): Fix false positive with asymmetric matche…
Browse files Browse the repository at this point in the history
…rs (#304)

* Expect parsing

* Get it working

* Fix up

* Tests

* Re-org
  • Loading branch information
mskelton authored Jul 1, 2024
1 parent 92e9240 commit 3860f1e
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 118 deletions.
37 changes: 25 additions & 12 deletions src/rules/no-conditional-expect.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import dedent from 'dedent'
import rule from '../../src/rules/no-conditional-expect'
import { runRuleTester } from '../utils/rule-tester'
import { javascript, runRuleTester } from '../utils/rule-tester'

const messageId = 'conditionalExpect'

runRuleTester('common tests', rule, {
invalid: [],
valid: [
`
javascript`
test('foo', () => {
expect(1).toBe(2);
});
`,
`
javascript`
test('foo', () => {
expect(!true).toBe(false);
});
`,
{
code: javascript`
test('foo', () => {
const expected = arr.map((x) => {
if (typeof x === 'string') {
return expect.arrayContaining([x])
} else {
return b
}
})
expect([1, 2, 3]).toEqual(expected);
});
`,
},
],
})

Expand Down Expand Up @@ -154,7 +168,7 @@ runRuleTester('logical conditions', rule, {
`,
// Global aliases
{
code: dedent`
code: javascript`
it('foo', () => {
process.env.FAIL && setNum(1);
Expand Down Expand Up @@ -331,7 +345,6 @@ runRuleTester('catch conditions', rule, {
code: `
test('foo', () => {
try {
} catch (err) {
expect(err).toMatch('Error');
}
Expand Down Expand Up @@ -397,7 +410,7 @@ runRuleTester('catch conditions', rule, {
runRuleTester('promises', rule, {
invalid: [
{
code: dedent`
code: javascript`
test('works', async () => {
await Promise.resolve()
.then(() => { throw new Error('oh noes!'); })
Expand All @@ -407,7 +420,7 @@ runRuleTester('promises', rule, {
errors: [{ messageId }],
},
{
code: dedent`
code: javascript`
test('works', async () => {
await Promise.resolve()
.then(() => { throw new Error('oh noes!'); })
Expand All @@ -421,7 +434,7 @@ runRuleTester('promises', rule, {
errors: [{ messageId }],
},
{
code: dedent`
code: javascript`
test('works', async () => {
await Promise.resolve()
.catch(error => expect(error).toBeInstanceOf(Error))
Expand All @@ -432,7 +445,7 @@ runRuleTester('promises', rule, {
errors: [{ messageId }],
},
{
code: dedent`
code: javascript`
test('works', async () => {
await Promise.resolve()
.catch(error => expect(error).toBeInstanceOf(Error))
Expand All @@ -444,7 +457,7 @@ runRuleTester('promises', rule, {
errors: [{ messageId }],
},
{
code: dedent`
code: javascript`
test('works', async () => {
await somePromise
.then(() => { throw new Error('oh noes!'); })
Expand All @@ -454,7 +467,7 @@ runRuleTester('promises', rule, {
errors: [{ messageId }],
},
{
code: dedent`
code: javascript`
test('works', async () => {
await somePromise.catch(error => expect(error).toBeInstanceOf(Error));
});
Expand Down
5 changes: 5 additions & 0 deletions src/rules/valid-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ runRuleTester('valid-expect', rule, {
valid: [
{ code: 'expectPayButtonToBeEnabled()' },
{ code: 'expect("something").toBe("else")' },
{ code: 'expect.anything()' },
{ code: 'expect.arrayContaining()' },
{ code: 'expect.not.arrayContaining()' },
{ code: 'expect.objectContaining(expected)' },
{ code: 'expect.not.objectContaining(expected)' },
{ code: 'expect("something").toBe(expect.anything())' },
{ code: 'expect("something").toEqual({ foo: expect.anything() })' },
{ code: 'expect("something").toBe(expect.arrayContaining([1, 2, 3]))' },
Expand Down
95 changes: 13 additions & 82 deletions src/utils/parseFnCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,36 +260,6 @@ runRuleTester('expect', rule, {
code: dedent`
import { expect } from '@playwright/test';
expect.assertions();
`,
errors: [
{
column: 1,
data: expectedParsedFnCallResultData({
args: [],
group: 'expect',
head: {
local: 'expect',
node: 'expect',
original: null,
},
matcher: 'assertions',
matcherArgs: [],
matcherName: 'assertions',
members: ['assertions'],
modifiers: [],
name: 'expect',
type: 'expect',
}),
line: 3,
messageId: 'details',
},
],
},
{
code: dedent`
import { expect } from '@playwright/test';
expect(x).toBe(y);
`,
errors: [
Expand Down Expand Up @@ -380,58 +350,6 @@ runRuleTester('expect', rule, {
code: dedent`
import { expect } from '@playwright/test';
expect.anything();
expect.not.arrayContaining();
`,
errors: [
{
column: 1,
data: expectedParsedFnCallResultData({
args: [],
group: 'expect',
head: {
local: 'expect',
node: 'expect',
original: null,
},
matcher: 'anything',
matcherArgs: [],
matcherName: 'anything',
members: ['anything'],
modifiers: [],
name: 'expect',
type: 'expect',
}),
line: 3,
messageId: 'details',
},
{
column: 1,
data: expectedParsedFnCallResultData({
args: [],
group: 'expect',
head: {
local: 'expect',
node: 'expect',
original: null,
},
matcher: 'arrayContaining',
matcherArgs: [],
matcherName: 'arrayContaining',
members: ['not', 'arrayContaining'],
modifiers: ['not'],
name: 'expect',
type: 'expect',
}),
line: 4,
messageId: 'details',
},
],
},
{
code: dedent`
import { expect } from '@playwright/test';
expect;
expect(x);
expect(x).toBe;
Expand Down Expand Up @@ -1187,5 +1105,18 @@ runTSRuleTester('typescript', rule, {
},
"it('is not a function', () => {});",
'dedent()',
'expect.assertions()',
'expect.anything()',
'expect.arrayContaining()',
'expect.objectContaining(expected)',
'expect.not.objectContaining(expected)',
{
code: dedent`
import { expect } from '@playwright/test';
expect.assertions();
expect.anything();
`,
},
],
})
Loading

0 comments on commit 3860f1e

Please sign in to comment.