Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

jest/expect-expect

Require every test to make at least one assertion

A test with no assertion passes as long as the code doesn't throw, whether the result is right or wrong. Usually someone forgot the expect, or lost it while changing the test.

test.todo isn't checked. test.skip is.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"jest/expect-expect" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"jest/expect-expect" = ["error", {
  assertFunctionNames = ["expect"],
  additionalTestBlockFunctions = [], # list of strings
}]

Examples

Reported:

import { test } from "@jest/globals";

test("parses the port", () => {
  parseConfig("port = 3000");
});

test.skip("parses the host", () => {
  parseConfig("host = localhost");
});

Not reported:

import { expect, test } from "@jest/globals";

test("parses the port", () => {
  expect(parseConfig("port = 3000")).toEqual({ port: 3000 });
});

test.todo("parses the host");

Options

OptionDefaultWhat it does
assertFunctionNames["expect"]The functions that count as an assertion. Your list replaces the default, so keep "expect" in it.
additionalTestBlockFunctions[]Other Jest functions to check like test, such as "afterEach".

assertFunctionNames

The rule doesn't look inside the helpers that a test calls. This test is reported even though checkUser calls expect:

import { expect, test } from "@jest/globals";

function checkUser(user) {
  expect(user.id).toBeGreaterThan(0);
}

test("creates a user", () => {
  checkUser(createUser());
});

Add the helper's name and the report goes away:

bunfig.toml
[lint.rules]
"jest/expect-expect" = ["error", {
  assertFunctionNames = ["expect", "checkUser"],
}]
import { expect, test } from "@jest/globals";

function checkUser(user) {
  expect(user.id).toBeGreaterThan(0);
}

test("creates a user", () => {
  checkUser(createUser());
});

A name can be a chain, such as request.get.expect. * matches any letters and digits in one part of the chain, and ** matches one or more parts. Case is ignored.

With "request.**.expect" in the list, this isn't reported:

import { test } from "@jest/globals";

test("is healthy", async () => {
  await request(app).get("/health").expect(200);
});

additionalTestBlockFunctions

This option only works for functions that come from Jest. With ["afterEach"], this is reported:

import { afterEach } from "@jest/globals";

afterEach(() => {
  closeDatabase();
});

Your own wrappers aren't checked, even if you list them. With ["testWithDatabase"], this isn't reported:

import { afterAll } from "@jest/globals";
import { closeDatabase, testWithDatabase } from "./helpers";

afterAll(closeDatabase);

testWithDatabase("saves the user", (db) => {
  db.save(createUser());
});