close

valid-expect

Added in v0.8.1

Configuration

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

export default defineConfig([
  rstestPlugin.configs.recommended,
  {
    rules: {
      'rstest/valid-expect': 'error',
    },
  },
]);

Rule Details

Validates Rstest expect calls: they need the required arguments and a matcher, and asynchronous assertions must be awaited or returned so failures are not lost.

Rstest supports a message as the second expect argument and Chai-style property assertions; both are accepted by the rule.

Incorrect

expect(user);

test('loads a user', async () => {
  expect(loadUser()).resolves.toMatchObject({ name: 'Ada' });
});

Correct

expect(user).toMatchObject({ name: 'Ada' });

test('loads a user', async () => {
  await expect(loadUser()).resolves.toMatchObject({ name: 'Ada' });
});

Options

{
  "rstest/valid-expect": [
    "error",
    {
      "alwaysAwait": true,
      "asyncMatchers": ["toReject", "toResolve", "toSettle"],
      "maxArgs": 2
    }
  ]
}
OptionTypeDefaultDescription
alwaysAwaitbooleanfalseRequire async assertions to be awaited instead of allowing return.
asyncMatchersstring[]["toReject", "toResolve"]Matcher names treated as asynchronous. Setting it replaces the default list.
minArgsnumber1Minimum number of arguments for expect.
maxArgsnumber1Maximum number of arguments for expect.

Autofix

An async assertion that is neither awaited nor returned is fixed by adding await, and by marking the enclosing function async when it is not already. Argument-count and matcher problems are reported without a fix.