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
167 changes: 167 additions & 0 deletions src/rules/__tests__/no-duplicate-hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,170 @@ ruleTester.run('nested describe blocks', rule, {
},
],
});

ruleTester.run('describe.each blocks', rule, {
valid: [
dedent`
describe.each(['hello'])('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});
`,
dedent`
describe('something', () => {
describe.each(['hello'])('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});

describe.each(['world'])('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});
});
`,
dedent`
describe.each\`\`('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});
`,
dedent`
describe('something', () => {
describe.each\`\`('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});

describe.each\`\`('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});
});
`,
],
invalid: [
{
code: dedent`
describe.each(['hello'])('%s', () => {
beforeEach(() => {});
beforeEach(() => {});

it('is not fine', () => {});
});
`,
errors: [
{
messageId: 'noDuplicateHook',
data: { hook: 'beforeEach' },
column: 3,
line: 3,
},
],
},
{
code: dedent`
describe('something', () => {
describe.each(['hello'])('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});

describe.each(['world'])('%s', () => {
beforeEach(() => {});
beforeEach(() => {});

it('is not fine', () => {});
});
});
`,
errors: [
{
messageId: 'noDuplicateHook',
data: { hook: 'beforeEach' },
column: 5,
line: 10,
},
],
},
{
code: dedent`
describe('something', () => {
describe.each(['hello'])('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});

describe.each(['world'])('%s', () => {
describe('some more', () => {
beforeEach(() => {});
beforeEach(() => {});

it('is not fine', () => {});
});
});
});
`,
errors: [
{
messageId: 'noDuplicateHook',
data: { hook: 'beforeEach' },
column: 7,
line: 11,
},
],
},
{
code: dedent`
describe.each\`\`('%s', () => {
beforeEach(() => {});
beforeEach(() => {});

it('is fine', () => {});
});
`,
errors: [
{
messageId: 'noDuplicateHook',
data: { hook: 'beforeEach' },
column: 3,
line: 3,
},
],
},
{
code: dedent`
describe('something', () => {
describe.each\`\`('%s', () => {
beforeEach(() => {});

it('is fine', () => {});
});

describe.each\`\`('%s', () => {
beforeEach(() => {});
beforeEach(() => {});

it('is not fine', () => {});
});
});
`,
errors: [
{
messageId: 'noDuplicateHook',
data: { hook: 'beforeEach' },
column: 5,
line: 10,
},
],
},
],
});
4 changes: 2 additions & 2 deletions src/rules/no-duplicate-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRule, isDescribe, isHook } from './utils';
import { createRule, isDescribe, isEachCall, isHook } from './utils';

const newHookContext = () => ({
beforeAll: 0,
Expand Down Expand Up @@ -45,7 +45,7 @@ export default createRule({
}
},
'CallExpression:exit'(node) {
if (isDescribe(node)) {
if (isDescribe(node) && !isEachCall(node)) {
hookContexts.pop();
}
},
Expand Down