Skip to content
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

fix: improve handling of .each calls and with tagged literals #814

Merged
merged 7 commits into from
Apr 26, 2021
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
4 changes: 2 additions & 2 deletions src/rules/__tests__/lowercase-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ ruleTester.run('lowercase-name', rule, {
{
messageId: 'unexpectedLowercase',
data: { method: TestCaseName.it },
column: 9,
column: 29,
line: 1,
},
],
Expand All @@ -203,7 +203,7 @@ ruleTester.run('lowercase-name', rule, {
{
messageId: 'unexpectedLowercase',
data: { method: DescribeAlias.describe },
column: 15,
column: 35,
line: 1,
},
],
Expand Down
65 changes: 65 additions & 0 deletions src/rules/__tests__/no-conditional-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ ruleTester.run('logical conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each()('foo', () => {
something || expect(something).toHaveBeenCalled();
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down Expand Up @@ -218,6 +226,14 @@ ruleTester.run('conditional conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each()('foo', () => {
something ? noop() : expect(something).toHaveBeenCalled();
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down Expand Up @@ -304,6 +320,19 @@ ruleTester.run('switch conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each()('foo', () => {
switch(something) {
case 'value':
expect(something).toHaveBeenCalled();
default:
break;
}
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down Expand Up @@ -384,6 +413,18 @@ ruleTester.run('if conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each()('foo', () => {
if(!doSomething) {
// do nothing
} else {
expect(something).toHaveBeenCalled();
}
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down Expand Up @@ -491,6 +532,18 @@ ruleTester.run('catch conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.each()('foo', () => {
try {

} catch (err) {
expect(err).toMatch('Error');
}
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.skip.each\`\`('foo', () => {
Expand All @@ -503,6 +556,18 @@ ruleTester.run('catch conditions', rule, {
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
it.skip.each()('foo', () => {
try {

} catch (err) {
expect(err).toMatch('Error');
}
})
`,
errors: [{ messageId: 'conditionalExpect' }],
},
{
code: `
function getValue() {
Expand Down
16 changes: 8 additions & 8 deletions src/rules/__tests__/no-focused-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ruleTester.run('no-focused-tests', rule, {
],
},
{
code: 'describe.only.each()',
code: 'describe.only.each()()',
errors: [
{
messageId: 'focusedTest',
Expand All @@ -55,7 +55,7 @@ ruleTester.run('no-focused-tests', rule, {
suggestions: [
{
messageId: 'suggestRemoveFocus',
output: 'describe.each()',
output: 'describe.each()()',
},
],
},
Expand Down Expand Up @@ -126,7 +126,7 @@ ruleTester.run('no-focused-tests', rule, {
],
},
{
code: 'it.only.each()',
code: 'it.only.each()()',
errors: [
{
messageId: 'focusedTest',
Expand All @@ -135,7 +135,7 @@ ruleTester.run('no-focused-tests', rule, {
suggestions: [
{
messageId: 'suggestRemoveFocus',
output: 'it.each()',
output: 'it.each()()',
},
],
},
Expand Down Expand Up @@ -206,7 +206,7 @@ ruleTester.run('no-focused-tests', rule, {
],
},
{
code: 'test.only.each()',
code: 'test.only.each()()',
errors: [
{
messageId: 'focusedTest',
Expand All @@ -215,7 +215,7 @@ ruleTester.run('no-focused-tests', rule, {
suggestions: [
{
messageId: 'suggestRemoveFocus',
output: 'test.each()',
output: 'test.each()()',
},
],
},
Expand Down Expand Up @@ -286,7 +286,7 @@ ruleTester.run('no-focused-tests', rule, {
],
},
{
code: 'fit.each()',
code: 'fit.each()()',
errors: [
{
messageId: 'focusedTest',
Expand All @@ -295,7 +295,7 @@ ruleTester.run('no-focused-tests', rule, {
suggestions: [
{
messageId: 'suggestRemoveFocus',
output: 'it.each()',
output: 'it.each()()',
},
],
},
Expand Down
47 changes: 47 additions & 0 deletions src/rules/__tests__/no-if.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ ruleTester.run('conditional expressions', rule, {
};
});
`,
dedent`
it.each()('foo', function () {
const foo = function (bar) {
return foo ? bar : null;
};
});
`,
],
invalid: [
{
Expand Down Expand Up @@ -120,6 +127,11 @@ ruleTester.run('switch statements', rule, {
switch('bar') {}
})
`,
dedent`
describe.skip.each()('foo', () => {
switch('bar') {}
})
`,
dedent`
xdescribe('foo', () => {
switch('bar') {}
Expand Down Expand Up @@ -501,6 +513,13 @@ ruleTester.run('if statements', rule, {
});
})
`,
dedent`
describe.each\`\`('foo', () => {
afterEach(() => {
if('bar') {}
});
})
`,
dedent`
describe('foo', () => {
beforeEach(() => {
Expand Down Expand Up @@ -826,6 +845,20 @@ ruleTester.run('if statements', rule, {
},
],
},
{
code: dedent`
it.each()('foo', () => {
callExpression()
if ('bar') {}
})
`,
errors: [
{
data: { condition: 'if' },
messageId: 'conditionalInTest',
},
],
},
{
code: dedent`
it.only.each\`\`('foo', () => {
Expand All @@ -840,6 +873,20 @@ ruleTester.run('if statements', rule, {
},
],
},
{
code: dedent`
it.only.each()('foo', () => {
callExpression()
if ('bar') {}
})
`,
errors: [
{
data: { condition: 'if' },
messageId: 'conditionalInTest',
},
],
},
{
code: dedent`
describe('valid', () => {
Expand Down
16 changes: 16 additions & 0 deletions src/rules/__tests__/no-test-return-statement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ ruleTester.run('no-test-return-statement', rule, {
`,
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
},
{
code: dedent`
it.each()("one", function () {
return expect(1).toBe(1);
});
`,
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
},
{
code: dedent`
it.only.each\`\`("one", function () {
Expand All @@ -76,6 +84,14 @@ ruleTester.run('no-test-return-statement', rule, {
`,
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
},
{
code: dedent`
it.only.each()("one", function () {
return expect(1).toBe(1);
});
`,
errors: [{ messageId: 'noReturnValue', column: 3, line: 2 }],
},
{
code: dedent`
it("one", myTest);
Expand Down
11 changes: 9 additions & 2 deletions src/rules/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const rule = createRule({
messages: {
details: [
'callType', //
'numOfArgs',
]
.map(data => `${data}: {{ ${data} }}`)
.join('\n'),
Expand All @@ -38,7 +39,10 @@ const rule = createRule({
context.report({
messageId: 'details',
node,
data: { callType },
data: {
callType,
numOfArgs: node.arguments.length,
},
});
}
},
Expand Down Expand Up @@ -86,7 +90,10 @@ const testUtilsAgainst = (
errors: [
{
messageId: 'details' as const,
data: { callType },
data: {
callType,
numOfArgs: 2,
},
column: 1,
line: 1,
},
Expand Down
5 changes: 3 additions & 2 deletions src/rules/consistent-test-it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
createRule,
getNodeName,
isDescribeCall,
isEachCall,
isTestCaseCall,
} from './utils';

Expand Down Expand Up @@ -86,6 +85,8 @@ export default createRule<
const funcNode =
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression
? node.callee.tag
: node.callee.type === AST_NODE_TYPES.CallExpression
? node.callee.callee
: node.callee;

if (
Expand Down Expand Up @@ -121,7 +122,7 @@ export default createRule<
}
},
'CallExpression:exit'(node) {
if (isDescribeCall(node) && !isEachCall(node)) {
if (isDescribeCall(node)) {
describeNestingLevel--;
}
},
Expand Down
Loading