Back to Biomejs

noDuplicateTestHooks

src/content/docs/linter/rules/no-duplicate-test-hooks.mdx

latest6.7 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.6.0` - Diagnostic Category: [`lint/suspicious/noDuplicateTestHooks`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - This rule belongs to the following domains: - [`test`](/linter/domains#test) - Sources: - Inspired from [`jest/no-duplicate-hooks`](https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/no-duplicate-hooks.md) - Inspired from [`vitest/no-duplicate-hooks`](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-duplicate-hooks.md)

How to configure

json
{
	"linter": {
		"rules": {
			"suspicious": {
				"noDuplicateTestHooks": "error"
			}
		}
	}
}

Description

A describe block should not contain duplicate hooks.

Examples

Invalid

js
describe('foo', () => {
  beforeEach(() => {
    // some setup
  });
  beforeEach(() => {
    // some setup
  });
  test('foo_test', () => {
   // some test
  });
});
<pre class="language-text"><code class="language-text">code-block.js:5:3 <a href="https://biomejs.dev/linter/rules/no-duplicate-test-hooks">lint/suspicious/noDuplicateTestHooks</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Duplicate </span><span style="color: Tomato;"><strong>beforeEach</strong></span><span style="color: Tomato;"> hook found.</span> <strong>3 │ </strong> // some setup <strong>4 │ </strong> &#125;); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong> beforeEach(() =&gt; &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>6 │ </strong> // some setup <strong><span style="color: Tomato;">&gt;</span></strong> <strong>7 │ </strong> &#125;); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>8 │ </strong> test('foo&#95;test', () =&gt; &#123; <strong>9 │ </strong> // some test <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Remove this duplicate hook or consolidate the logic into a single hook.</span> </code></pre>
js
describe('foo', () => {
  beforeEach(() => {
    // some setup
  });
  test('foo_test', () => {
    afterAll(() => {
      // some teardown
    });
   afterAll(() => {
     // some teardown
   });
  });
});
<pre class="language-text"><code class="language-text">code-block.js:9:4 <a href="https://biomejs.dev/linter/rules/no-duplicate-test-hooks">lint/suspicious/noDuplicateTestHooks</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Duplicate </span><span style="color: Tomato;"><strong>afterAll</strong></span><span style="color: Tomato;"> hook found.</span> <strong>7 │ </strong> // some teardown <strong>8 │ </strong> &#125;); <strong><span style="color: Tomato;">&gt;</span></strong> <strong>9 │ </strong> afterAll(() =&gt; &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>10 │ </strong> // some teardown <strong><span style="color: Tomato;">&gt;</span></strong> <strong>11 │ </strong> &#125;); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>12 │ </strong> &#125;); <strong>13 │ </strong>&#125;); <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Remove this duplicate hook or consolidate the logic into a single hook.</span> </code></pre>

Valid

js
describe('foo', () => {
  beforeEach(() => {
    // some setup
  });
  test('foo_test', () => {
    // some test
  });
});
</TabItem> </Tabs>