Skip to content

Improve expect parsing #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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