Skip to content
โ† Back to rules

jest/no-hooks Style

What it does โ€‹

Disallows Jest setup and teardown hooks, such as beforeAll.

Why is this bad? โ€‹

Jest provides global functions for setup and teardown tasks, which are called before/after each test case and each test suite. The use of these hooks promotes shared state between tests.

This rule reports for the following function calls:

  • beforeAll
  • beforeEach
  • afterAll
  • afterEach

Examples โ€‹

Examples of incorrect code for this rule:

javascript
function setupFoo(options) {
  /* ... */
}
function setupBar(options) {
  /* ... */
}

describe("foo", () => {
  let foo;
  beforeEach(() => {
    foo = setupFoo();
  });
  afterEach(() => {
    foo = null;
  });
  it("does something", () => {
    expect(foo.doesSomething()).toBe(true);
  });
  describe("with bar", () => {
    let bar;
    beforeEach(() => {
      bar = setupBar();
    });
    afterEach(() => {
      bar = null;
    });
    it("does something with bar", () => {
      expect(foo.doesSomething(bar)).toBe(true);
    });
  });
});

Configuration โ€‹

This rule accepts a configuration object with the following properties:

allow โ€‹

type: string[]

default: []

An array of hook function names that are permitted for use.

How to use โ€‹

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-hooks": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["jest"],
  rules: {
    "jest/no-hooks": "error",
  },
});
bash
oxlint --deny jest/no-hooks --jest-plugin

Version โ€‹

This rule was added in v0.0.16.

References โ€‹