{"record":{"id":"3e2baba8a2dac786","repo":"oxc-project/oxc","slug":"expect-is-shadowed-by-a-callback-parameter-and-c","errorCode":null,"errorMessage":"`expect` is shadowed by a callback parameter and cannot be used for assertions.","messagePattern":"`expect` is shadowed by a callback parameter and cannot be used for assertions\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/prefer_expect_assertions.rs","lineNumber":160,"sourceCode":"  fetchData((data) => {\n    expect(data).toBe('peanut butter');\n  });\n});\n```\n\nExamples of **correct** code with `{ \"onlyFunctionsWithExpectInCallback\": true }`:\n```javascript\ntest('callback test', () => {\n  expect.assertions(1);\n  fetchData((data) => {\n    expect(data).toBe('peanut butter');\n  });\n});\n```\n\"#;\n\nfn expect_shadowed_by_parameter(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"`expect` is shadowed by a callback parameter and cannot be used for assertions.\",\n    )\n    .with_help(\"Rename the parameter to avoid shadowing the global `expect`.\")\n    .with_label(span)\n}\n\npub fn has_assertions_takes_no_arguments(span: Span, prefix: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"`{prefix}.hasAssertions` expects no arguments.\"))\n        .with_help(format!(\"Remove the arguments from `{prefix}.hasAssertions()`.\"))\n        .with_label(span)\n}\n\nfn assertions_requires_one_argument(span: Span, prefix: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"`{prefix}.assertions` expects a single argument of type number.\"))\n        .with_help(format!(\"Pass a single numeric argument to `{prefix}.assertions()`.\"))\n        .with_label(span)\n}\n","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/prefer_expect_assertions.rs#L142-L178","documentation":"This diagnostic comes from the oxlint `prefer-expect-assertions` rule. When the rule tries to resolve which `expect` a test uses (file-level import prefix vs global), it walks the test callback; if a callback parameter is named `expect` (shadowing the global/imported one), the resolver returns None and the rule reports that it cannot reason about assertions. Shadowing also means calls inside that callback do not run the real Jest expect, so `expect.assertions(n)` bookkeeping would be wrong anyway.","triggerScenarios":"A test whose callback — or a nested callback passed to the function under test — declares a parameter named `expect`, e.g. `runScenario((...args, expect) => ...)` or `test('x', () => { fn((expect) => {}); })`, causing `resolve_expect` to fail and the rule to emit this diagnostic on the test callee span.","commonSituations":"Testing libraries whose API passes an assertion object to callbacks (e.g. `test('x', ({ expect }) => ...)` in some frameworks); renaming a callback parameter during refactoring to `expect`; porting tests from frameworks where `expect` is injected (node:test style) into Jest/Vitest.","solutions":["Rename the shadowing parameter (e.g. `expect` → `assert` or `ctx`) at both the declaration and its uses.","If using a framework that injects `expect` as a destructured parameter, disable this rule for those files or configure the rule off, since the injected expect is intentional.","For node:test-style code migrating to Jest, replace the injected parameter with the global/imported `expect`."],"exampleFix":"// before\ntest('scenario', () => {\n  runScenario((payload, expect) => {\n    expect(payload).toBe('ok');\n  });\n});\n\n// after\ntest('scenario', () => {\n  runScenario((payload, assert) => {\n    expect(payload).toBe('ok');\n  });\n});","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n{ \"rules\": { \"jest/prefer-expect-assertions\": \"error\" } }\n\nnpx oxlint tests/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never name callback parameters `expect` in Jest/Vitest suites; reserve the identifier for the global.","If your framework injects expect, disable this rule for those files with an inline oxlint-disable comment and a reason.","Destructure injected contexts under a different name (ctx, t) when porting node:test code."],"tags":["jest","vitest","oxlint","testing","variable-shadowing","static-analysis"],"backgroundTag":"variable-shadowing","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}