Why is this an issue?

Assertions check whether actual test results match expected outcomes. By using assertions, developers ensure code behaves as intended and catch potential bugs early.

An incomplete assertion is an assertion statement that lacks necessary matcher calls or conditions. In testing frameworks like Jest, Vitest, Bun (bun:test), @testing-library/jest-dom, @playwright/test, Node.js assert, and Chai, accessing a matcher method or chain modifier without invoking it evaluates silently as a no-op without running any validation.

This rule flags incomplete assertions in the following cases:

Code examples

Noncompliant code example

import { expect, test } from 'vitest';
import { screen } from '@testing-library/dom';
import '@testing-library/jest-dom';
import assert from 'node:assert';

test('incomplete assertions', async () => {
  expect(actual).toBe; // Noncompliant: 'toBe' matcher is not called
  expect(fn).toThrow; // Noncompliant: 'toThrow' matcher is not called
  expect(spy).toHaveBeenCalled; // Noncompliant: 'toHaveBeenCalled' matcher is not called
  expect(screen.getByText('Submit')).toBeInTheDocument; // Noncompliant: 'toBeInTheDocument' matcher is not called

  expect(value).not; // Noncompliant: 'not' modifier is not followed by a matcher
  expect(promise).resolves; // Noncompliant: 'resolves' modifier is not followed by a matcher
  expect(actual); // Noncompliant: 'expect' is not followed by a matcher

  assert.strictEqual; // Noncompliant: 'strictEqual' method is not called
});

Compliant solution

Make sure to write complete assertions by calling matcher functions and appending appropriate assertions to modifiers.

import { expect, test } from 'vitest';
import { screen } from '@testing-library/dom';
import '@testing-library/jest-dom';
import assert from 'node:assert';

test('complete assertions', async () => {
  expect(actual).toBe(expected);
  expect(fn).toThrow(TypeError);
  expect(spy).toHaveBeenCalled();
  expect(screen.getByText('Submit')).toBeInTheDocument();

  expect(value).not.toBeNull();
  await expect(promise).resolves.toBe(42);
  expect(actual).toEqual(expected);

  assert.strictEqual(actual, expected);
});

Noncompliant code example

const { expect } = require('chai');

expect(value).to.equal; // Noncompliant: 'equal' matcher is not called

Compliant solution

const { expect } = require('chai');

expect(value).to.equal(expected);

Resources

Documentation