oxc-project/oxc · error
Expect must have a corresponding matcher call.
Error message
Expect must have a corresponding matcher call.
What it means
Diagnostic from the oxlint `jest/valid-expect` rule (source: crates/oxc_linter/src/rules/shared/jest_vitest/valid_expect.rs:23). This message ('Expect must have a corresponding matcher call.') fires when `expect(value)` is never chained to a matcher - the expression's value is discarded, so the assertion never runs and the test passes vacuously. The rule uses `ExpectError`-style helpers and the shared diagnostic builder in the source.
Source
Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/valid_expect.rs:23
use oxc_semantic::ScopeId;
use oxc_span::{GetSpan, Span};
use rustc_hash::FxHashSet;
use schemars::JsonSchema;
use crate::{
AstNode,
context::LintContext,
utils::{
ExpectError, PossibleJestNode, collect_possible_jest_call_node, parse_expect_jest_fn_call,
},
};
fn valid_expect_diagnostic<S: Into<Cow<'static, str>>>(
x1: S,
x2: &'static str,
span3: Span,
) -> OxcDiagnostic {
OxcDiagnostic::warn(x1).with_help(x2).with_label(span3)
}
pub const DOCUMENTATION: &str = r"### What it does
Checks that `expect()` is called correctly.
### Why is this bad?
`expect()` is a function that is used to assert values in tests.
It should be called with a single argument, which is the value to be tested.
If you call `expect()` with no arguments, or with more than one argument, it will not work as expected.
### Examples
Examples of **incorrect** code for this rule:
```javascript
expect();
expect('something');View on GitHub (pinned to e1e7af627c)
Solutions
- Complete the assertion: `expect(value).toBe(expected)` or whichever matcher applies.
- If the line was a debugging leftover, delete the bare `expect(...)`.
- For promise assertions use the full chain: `await expect(fetchUser()).resolves.toMatchObject({ id: 1 })`.
- Search for the pattern repo-wide with `rg 'expect\([^)]*\);\s*$'` to catch other occurrences.
Example fix
// before
expect(result.status);
// after
expect(result.status).toBe('ok'); Defensive patterns
Strategy: validation
Validate before calling
// rg -n "expect\([^)]*\);\s*$" tests/ -t ts -t js # bare expect statements
Type guard
// Guard for dynamic assertion helpers (JS):
function assertHasMatcher(fn) {
const src = fn.toString();
return !/^\s*expect\([^)]*\);\s*$/m.test(src);
} Prevention
- Never comment out just the matcher; comment the whole expect line
- Write the full chain on one statement before splitting lines
- Keep valid-expect enabled as an error so vacuous assertions fail lint
When it happens
Trigger: `expect(value);` as a standalone expression statement, `const p = expect(x);` never followed by `.toBe/...`, or an expect chain broken by a stray semicolon or early return before the matcher. Detected via `collect_possible_jest_call_node` + `parse_expect_jest_fn_call` finding no matcher.
Common situations: Commenting out a matcher while debugging and forgetting to restore it; refactors that split the chain across lines; await-less `expect(promise).resolves...` typos where the chain got truncated; copy-paste that dropped the second line.
Related errors
- Matchers must be called to assert.
- Expect has an unknown modifier.
- Async assertions must be awaited.
- Promises which return async assertions must be awaited.
- Expect takes at most {} argument{}
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/c085f4d899c1fe6e.
Report an issue: GitHub.