Skip to content
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
44 changes: 44 additions & 0 deletions src/rules/__tests__/no-done-callback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ ruleTester.run('no-done-callback', rule, {
'test("something", () => {})',
'test("something", async () => {})',
'test("something", function() {})',
'test.each``("something", ({ a, b }) => {})',
'test.each()("something", ({ a, b }) => {})',
'it.each()("something", ({ a, b }) => {})',
'it.each``("something", ({ a, b }) => {})',
'test("something", async function () {})',
'test("something", someArg)',
'beforeEach(() => {})',
Expand Down Expand Up @@ -385,5 +389,45 @@ ruleTester.run('no-done-callback', rule, {
},
],
},
{
code: 'test.each``("something", ({ a, b }, done) => { done(); })',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 37,
},
],
},
{
code: 'test.each()("something", ({ a, b }, done) => { done(); })',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 37,
},
],
},
{
code: 'it.each``("something", ({ a, b }, done) => { done(); })',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 35,
},
],
},
{
code: 'it.each()("something", ({ a, b }, done) => { done(); })',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 35,
},
],
},
],
});
23 changes: 19 additions & 4 deletions src/rules/no-done-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { createRule, isFunction, isHook, isTestCase } from './utils';
import {
createRule,
getNodeName,
isFunction,
isHook,
isTestCase,
} from './utils';

const findCallbackArg = (
node: TSESTree.CallExpression,
isJestEach: boolean,
): TSESTree.CallExpression['arguments'][0] | null => {
if (isJestEach) {
return node.arguments[1];
}

if (isHook(node) && node.arguments.length >= 1) {
return node.arguments[0];
}
Expand Down Expand Up @@ -41,17 +52,21 @@ export default createRule({
create(context) {
return {
CallExpression(node) {
const callback = findCallbackArg(node);
// done is the second argument for it.each, not the first
const isJestEach = getNodeName(node.callee)?.endsWith('.each') ?? false;

const callback = findCallbackArg(node, isJestEach);
const callbackArgIndex = Number(isJestEach);

if (
!callback ||
!isFunction(callback) ||
callback.params.length !== 1
callback.params.length !== 1 + callbackArgIndex
) {
return;
}

const [argument] = callback.params;
const argument = callback.params[callbackArgIndex];

if (argument.type !== AST_NODE_TYPES.Identifier) {
context.report({
Expand Down