Skip to content

Files

Latest commit

 

History

History
38 lines (30 loc) · 740 Bytes

no-conditional-expect.md

File metadata and controls

38 lines (30 loc) · 740 Bytes

Pattern: Conditional assertion

Issue: -

Description

Using expect in conditional blocks (if statements, catch blocks, or conditional callbacks) can result in tests that pass without actually testing anything, since Jest only fails a test when an error is thrown.

Examples

Example of incorrect code:

it("test", () => {
  if (condition) {
    expect(value).toBe(true);
  }
});

it("error", async () => {
  try {
    await operation();
  } catch (err) {
    expect(err.code).toBe("ERROR");
  }
});

Example of correct code:

it("test", () => {
  expect(condition).toBe(true);
  expect(value).toBe(true);
});

it("error", async () => {
  await expect(operation).rejects.toThrow("ERROR");
});