For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/jest/require-to-throw-message.md.
close

require-to-throw-message

Added in v0.7.3

Configuration

PresetConfigured Value
✅ jestPlugin.configs.all"error"
rslint.config.ts
import { defineConfig, jestPlugin } from '@rslint/core';

export default defineConfig([
  jestPlugin.configs.recommended,
  {
    rules: {
      'jest/require-to-throw-message': 'error',
    },
  },
]);

Rule Details

Require a message for toThrow() (and its alias toThrowError()).

toThrow() checks that a function throws, as in expect(() => a()).toThrow(). Without a message, the assertion passes for any thrown error. Requiring a message ensures the intended error is thrown.

This rule reports when toThrow() or toThrowError() is used with no argument. Negated forms such as .not.toThrow() without a message are allowed.

Examples of incorrect code for this rule:

test('all the things', async () => {
  expect(() => a()).toThrow();

  expect(() => a()).toThrowError();

  await expect(a()).rejects.toThrow();

  await expect(a()).rejects.toThrowError();
});

Examples of correct code for this rule:

test('all the things', async () => {
  expect(() => a()).toThrow('a');

  expect(() => a()).toThrowError('a');

  await expect(a()).rejects.toThrow('a');

  await expect(a()).rejects.toThrowError('a');

  expect(() => a()).not.toThrow();
});

Original Documentation