Skip to content

Commit 6cd1208

Browse files
committed
fix(no-duplicate-hooks): support describe.each
Fixes #642
1 parent e6b8c77 commit 6cd1208

File tree

2 files changed

+169
-2
lines changed

2 files changed

+169
-2
lines changed

src/rules/__tests__/no-duplicate-hooks.test.ts

+167
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,170 @@ ruleTester.run('nested describe blocks', rule, {
288288
},
289289
],
290290
});
291+
292+
ruleTester.run('describe.each blocks', rule, {
293+
valid: [
294+
dedent`
295+
describe.each(['hello'])('%s', () => {
296+
beforeEach(() => {});
297+
298+
it('is fine', () => {});
299+
});
300+
`,
301+
dedent`
302+
describe('something', () => {
303+
describe.each(['hello'])('%s', () => {
304+
beforeEach(() => {});
305+
306+
it('is fine', () => {});
307+
});
308+
309+
describe.each(['world'])('%s', () => {
310+
beforeEach(() => {});
311+
312+
it('is fine', () => {});
313+
});
314+
});
315+
`,
316+
dedent`
317+
describe.each\`\`('%s', () => {
318+
beforeEach(() => {});
319+
320+
it('is fine', () => {});
321+
});
322+
`,
323+
dedent`
324+
describe('something', () => {
325+
describe.each\`\`('%s', () => {
326+
beforeEach(() => {});
327+
328+
it('is fine', () => {});
329+
});
330+
331+
describe.each\`\`('%s', () => {
332+
beforeEach(() => {});
333+
334+
it('is fine', () => {});
335+
});
336+
});
337+
`,
338+
],
339+
invalid: [
340+
{
341+
code: dedent`
342+
describe.each(['hello'])('%s', () => {
343+
beforeEach(() => {});
344+
beforeEach(() => {});
345+
346+
it('is not fine', () => {});
347+
});
348+
`,
349+
errors: [
350+
{
351+
messageId: 'noDuplicateHook',
352+
data: { hook: 'beforeEach' },
353+
column: 3,
354+
line: 3,
355+
},
356+
],
357+
},
358+
{
359+
code: dedent`
360+
describe('something', () => {
361+
describe.each(['hello'])('%s', () => {
362+
beforeEach(() => {});
363+
364+
it('is fine', () => {});
365+
});
366+
367+
describe.each(['world'])('%s', () => {
368+
beforeEach(() => {});
369+
beforeEach(() => {});
370+
371+
it('is not fine', () => {});
372+
});
373+
});
374+
`,
375+
errors: [
376+
{
377+
messageId: 'noDuplicateHook',
378+
data: { hook: 'beforeEach' },
379+
column: 5,
380+
line: 10,
381+
},
382+
],
383+
},
384+
{
385+
code: dedent`
386+
describe('something', () => {
387+
describe.each(['hello'])('%s', () => {
388+
beforeEach(() => {});
389+
390+
it('is fine', () => {});
391+
});
392+
393+
describe.each(['world'])('%s', () => {
394+
describe('some more', () => {
395+
beforeEach(() => {});
396+
beforeEach(() => {});
397+
398+
it('is not fine', () => {});
399+
});
400+
});
401+
});
402+
`,
403+
errors: [
404+
{
405+
messageId: 'noDuplicateHook',
406+
data: { hook: 'beforeEach' },
407+
column: 7,
408+
line: 11,
409+
},
410+
],
411+
},
412+
{
413+
code: dedent`
414+
describe.each\`\`('%s', () => {
415+
beforeEach(() => {});
416+
beforeEach(() => {});
417+
418+
it('is fine', () => {});
419+
});
420+
`,
421+
errors: [
422+
{
423+
messageId: 'noDuplicateHook',
424+
data: { hook: 'beforeEach' },
425+
column: 3,
426+
line: 3,
427+
},
428+
],
429+
},
430+
{
431+
code: dedent`
432+
describe('something', () => {
433+
describe.each\`\`('%s', () => {
434+
beforeEach(() => {});
435+
436+
it('is fine', () => {});
437+
});
438+
439+
describe.each\`\`('%s', () => {
440+
beforeEach(() => {});
441+
beforeEach(() => {});
442+
443+
it('is not fine', () => {});
444+
});
445+
});
446+
`,
447+
errors: [
448+
{
449+
messageId: 'noDuplicateHook',
450+
data: { hook: 'beforeEach' },
451+
column: 5,
452+
line: 10,
453+
},
454+
],
455+
},
456+
],
457+
});

src/rules/no-duplicate-hooks.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createRule, isDescribe, isHook } from './utils';
1+
import { createRule, isDescribe, isEachCall, isHook } from './utils';
22

33
const newHookContext = () => ({
44
beforeAll: 0,
@@ -45,7 +45,7 @@ export default createRule({
4545
}
4646
},
4747
'CallExpression:exit'(node) {
48-
if (isDescribe(node)) {
48+
if (isDescribe(node) && !isEachCall(node)) {
4949
hookContexts.pop();
5050
}
5151
},

0 commit comments

Comments
 (0)