Skip to content

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

Example ​

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);
    });
  });
});

How to use ​

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

bash
oxlint --deny jest/no-hooks --jest-plugin
json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-hooks": "error"
  }
}

References ​

Released under the MIT License.