{"record":{"id":"9de2529acafe1dd8","repo":"oxc-project/oxc","slug":"expect-must-be-inside-of-a-test-block","errorCode":null,"errorMessage":"`expect` must be inside of a test block.","messagePattern":"`expect` must be inside of a test block\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/no_standalone_expect.rs","lineNumber":23,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_semantic::NodeId;\nuse oxc_span::Span;\nuse oxc_str::CompactStr;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::DefaultRuleConfig,\n    utils::{\n        JestFnKind, JestGeneralFnKind, KnownMemberExpressionParentKind, ParsedExpectFnCall,\n        PossibleJestNode, collect_possible_jest_call_node, get_node_name,\n        parse_expect_jest_fn_call, parse_general_jest_fn_call,\n    },\n};\n\nfn no_standalone_expect_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"`expect` must be inside of a test block.\")\n        .with_help(\"Did you forget to wrap `expect` in a `test` or `it` block?\")\n        .with_label(span)\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n\nPrevents `expect` statements outside of a `test` or `it` block. An `expect`\nwithin a helper function (but outside of a `test` or `it` block) will not\ntrigger this rule.\n\nStatements like `expect.hasAssertions()` will NOT trigger this rule since these\ncalls will execute if they are not in a test block.\n\n### Why is this bad?\n\n`expect` statements outside of test blocks will not be executed by the Jest\ntest runner, which means they won't actually test anything. This can lead to\nfalse confidence in test coverage and may hide bugs that would otherwise be","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/no_standalone_expect.rs#L5-L41","documentation":"This is the oxlint `no-standalone-expect` rule (jest/vitest plugin), ported from eslint-plugin-jest. It fires when an `expect()` assertion call is made outside of any `test`/`it` block — for example directly in the module body or inside a `describe` callback. Such assertions are never executed by the test runner (or run at collection time), so they silently give false confidence in coverage. `expect` inside plain function declarations or variable-assigned helpers is intentionally allowed, and `expect.assertions()`/`expect.hasAssertions()` member calls are skipped because they legitimately run outside tests.","triggerScenarios":"A `parse_expect_jest_fn_call`-recognized expect call whose enclosing function chain never terminates at a test block: expect() at the top level of a file, or `describe('x', () => { expect(1).toBe(1); })` with no `it` wrapper. Also fires when a custom test wrapper (e.g. `testEachPlatform('x', () => {...})`) is used but is not listed in the rule's `additionalTestBlockFunctions` config array, so the linter cannot recognize it as a test block.","commonSituations":"Refactoring that hoists an assertion out of a test callback; using an in-house wrapper around `test`/`it` (custom framework, e.g. `itEach`, `testConcurrent`) that the linter does not know; migrating a codebase to oxlint where the previous linter had the wrapper names configured; writing sanity checks in a `describe` body assuming describe callbacks run per-test.","solutions":["Wrap the assertion in a `test(...)` or `it(...)` block.","If the assertion sits in a custom test-wrapper function, add that function name to the rule's `additionalTestBlockFunctions` option in .oxlintrc.json.","If the code is shared assertion logic, move it into a named helper function (function declaration or `const helper = () => ...`) and call it from inside tests — those are exempt by design.","Delete the assertion if it is leftover dead code from a refactor."],"exampleFix":"// before\ndescribe('math', () => {\n  expect(1 + 1).toBe(2);\n});\n\n// after\ndescribe('math', () => {\n  it('adds numbers', () => {\n    expect(1 + 1).toBe(2);\n  });\n});","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n{\n  \"plugins\": [\"jest\"],\n  \"rules\": {\n    \"jest/no-standalone-expect\": \"error\",\n    \"jest/no-standalone-expect\": [\"error\", { \"additionalTestBlockFunctions\": [\"itEachPlatform\"] }]\n  }\n}\n\n# preflight before commit\nnpx oxlint tests/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always author assertions inside an it/test callback; treat describe bodies as declaration-only zones.","Register every custom test wrapper in additionalTestBlockFunctions when you introduce it.","Run oxlint in a pre-commit hook or CI gate so a stray expect fails the build, not code review."],"tags":["jest","vitest","oxlint","testing","assertions","static-analysis"],"backgroundTag":"expect-outside-test","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"}