oxc-project/oxc · warning · OxcDiagnostic

Unexpected alias {name:?}

Error message

Unexpected alias {name:?}

What it means

Diagnostic from the oxlint rule `jest/no-alias-methods` (shared with vitest) in crates/oxc_linter/src/rules/shared/jest_vitest/no_alias_methods.rs:11. Jest matchers have legacy alias spellings (toBeCalled, lastCalledWith, toThrowError, ...) and canonical spellings (toHaveBeenCalled, toHaveBeenLastCalledWith, toThrow). The rule flags the alias on any expect() matcher chain and offers an automatic fix that replaces the alias with its canonical name.

Source

Thrown at crates/oxc_linter/src/rules/shared/jest_vitest/no_alias_methods.rs:11

use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;

use crate::{
    context::LintContext,
    utils::{PossibleJestNode, parse_expect_jest_fn_call},
};

fn no_alias_methods_diagnostic(name: &str, canonical_name: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Unexpected alias {name:?}"))
        .with_help(format!("Replace {name:?} with its canonical name of {canonical_name:?}"))
        .with_label(span)
}

pub fn run_on_jest_node<'a, 'c>(jest_node: &PossibleJestNode<'a, 'c>, ctx: &'c LintContext<'a>) {
    let node = jest_node.node;
    let AstKind::CallExpression(call_expr) = node.kind() else {
        return;
    };
    let Some(jest_fn_call) = parse_expect_jest_fn_call(call_expr, jest_node, ctx) else {
        return;
    };
    let Some(matcher) = jest_fn_call.matcher() else {
        return;
    };
    let Some(alias) = matcher.name() else {
        return;
    };

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Apply the built-in autofix: run `npx oxlint --fix` and every alias matcher is replaced with its canonical name
  2. Manually rename using the mapping in the help text, e.g. toBeCalledWith -> toHaveBeenCalledWith, lastCalledWith -> toHaveBeenLastCalledWith, toThrowError -> toThrow
  3. Standardize on canonical matchers in review checklists / editor snippets so aliases stop entering the codebase

Example fix

// before
expect(fn).toBeCalled();
expect(fn).lastCalledWith(1, 2);
expect(fn).toThrowError('boom');

// after
expect(fn).toHaveBeenCalled();
expect(fn).toHaveBeenLastCalledWith(1, 2);
expect(fn).toThrow('boom');
Defensive patterns

Strategy: validation

Validate before calling

rg -n "\.(toBeCalled|toBeCalledTimes|toBeCalledWith|lastCalledWith|nthCalledWith|toReturn|toReturnTimes|toReturnWith|lastReturnedWith|nthReturnedWith|toThrowError)\(" tests/

Prevention

When it happens

Trigger: Any expect(...) chain ending in one of the eleven aliases enumerated in BadAliasMethodName: toBeCalled, toBeCalledTimes, toBeCalledWith, lastCalledWith, nthCalledWith, toReturn, toReturnTimes, toReturnWith, lastReturnedWith, nthReturnedWith, toThrowError (including quoted matchers like expect(x)['toBeCalled'](), where only the name inside the quotes is replaced).

Common situations: Codebases mixing old Jest docs/examples with new ones; copy-paste from older tutorials; style divergence across a team where half use aliases; enabling the jest plugin in oxlint on an existing repo for the first time.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/b805f1ae8dd2e4fd. Report an issue: GitHub.