Skip to content

Commit

Permalink
fix(no-duplicate-hooks): support describe.each (#797)
Browse files Browse the repository at this point in the history
Fixes #642
  • Loading branch information
G-Rath authored Apr 2, 2021
1 parent 5945772 commit 243cb4f
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 2 deletions.
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

0 comments on commit 243cb4f

Please sign in to comment.