Skip to content

jest/consistent-test-it Style

🛠️ An auto-fix is available for this rule.

What it does

Jest allows you to choose how you want to define your tests, using the it or the test keywords, with multiple permutations for each:

  • it: it, xit, fit, it.only, it.skip.
  • test: test, xtest, test.only, test.skip.

Why is this bad?

It's a good practice to be consistent in your test suite, so that all tests are written in the same way.

Example

javascript
/*eslint jest/consistent-test-it: ["error", {"fn": "test"}]*/
test("foo"); // valid
test.only("foo"); // valid

it("foo"); // invalid
it.only("foo"); // invalid
javascript
/*eslint jest/consistent-test-it: ["error", {"fn": "it"}]*/
it("foo"); // valid
it.only("foo"); // valid
test("foo"); // invalid
test.only("foo"); // invalid
javascript
/*eslint jest/consistent-test-it: ["error", {"fn": "it", "withinDescribe": "test"}]*/
it("foo"); // valid
describe("foo", function () {
  test("bar"); // valid
});

test("foo"); // invalid
describe("foo", function () {
  it("bar"); // invalid
});

Options

This rule can be configured as follows

json5
{
  type: "object",
  properties: {
    fn: {
      enum: ["it", "test"],
    },
    withinDescribe: {
      enum: ["it", "test"],
    },
  },
  additionalProperties: false,
}
fn

Decides whether to use test or it.

withinDescribe

Decides whether to use test or it within a describe scope.

This rule is compatible with eslint-plugin-vitest, to use it, add the following configuration to your .eslintrc.json:

json
{
  "rules": {
     "vitest/consistent-test-it": "error"
  }
}


## References
- [Rule Source](https://github.com/oxc-project/oxc/blob/25ddb3519289dfe2a51e53b9b0c355d8ad950bbc/crates/oxc_linter/src/rules/jest/consistent_test_it.rs)

Released under the MIT License.