{"record":{"id":"b534128118d6074a","repo":"oxc-project/oxc","slug":"promises-which-return-async-assertions-must-be-awa","errorCode":null,"errorMessage":"Promises which return async assertions must be awaited.","messagePattern":"Promises which return async assertions must be awaited\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/shared/jest_vitest/valid_expect.rs","lineNumber":23,"sourceCode":"use oxc_semantic::ScopeId;\nuse oxc_span::{GetSpan, Span};\nuse rustc_hash::FxHashSet;\nuse schemars::JsonSchema;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    utils::{\n        ExpectError, PossibleJestNode, collect_possible_jest_call_node, parse_expect_jest_fn_call,\n    },\n};\n\nfn valid_expect_diagnostic<S: Into<Cow<'static, str>>>(\n    x1: S,\n    x2: &'static str,\n    span3: Span,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(x1).with_help(x2).with_label(span3)\n}\n\npub const DOCUMENTATION: &str = r\"### What it does\n\nChecks that `expect()` is called correctly.\n\n### Why is this bad?\n\n`expect()` is a function that is used to assert values in tests.\nIt should be called with a single argument, which is the value to be tested.\nIf you call `expect()` with no arguments, or with more than one argument, it will not work as expected.\n\n### Examples\n\nExamples of **incorrect** code for this rule:\n```javascript\nexpect();\nexpect('something');","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/shared/jest_vitest/valid_expect.rs#L5-L41","documentation":"Diagnostic from the shared jest/vitest `valid-expect` rule (Message::PromisesWithAsyncAssertionsMustBeAwaited). It is the chained variant of the await check: the async assertion sits inside or at the end of a `.then()`/`.catch()` (or Promise.all-style) chain, and that whole chain is floating — not awaited or returned — so the assertion may never execute.","triggerScenarios":"`get_parent_if_thenable`/`find_promise_call_expression_node` walk past `then`/`catch` links; when the final chain node differs from the assertion node (`target_node.id() != final_node.id()`) and the chain's parent is not an acceptable await/return position, this message is emitted. Examples: `Promise.resolve().then(() => expect(p).resolves.toBe(1));`, or `promise.catch(() => expect(x).toResolve());` left as a statement.","commonSituations":"Refactoring `promise.then(...)`-style tests into modern async/await without deleting the chain; assertions appended inside `.catch()` callbacks for error-path checks; Promise.race/all wrappers around async assertions.","solutions":["`await` (or `return`) the entire chain: `await Promise.resolve().then(() => expect(p).resolves.toBe(1));`.","Better: rewrite the chain as `await expect(p).resolves.toBe(1)` directly, dropping the then/catch wrapper.","Move the assertion out of the callback and assert on the awaited result: `const v = await p; expect(v).toBe(1);`.","Run `oxlint --fix`; the rule's multifix adds `await` and `async` where safe."],"exampleFix":"// before\nit('works', () => {\n  Promise.resolve(2).then((v) => expect(Promise.resolve(v)).resolves.toBe(2));\n});\n\n// after\nit('works', async () => {\n  await Promise.resolve(2).then((v) => expect(Promise.resolve(v)).resolves.toBe(2));\n});","handlingStrategy":"validation","validationCode":"// flag expect() inside .then/.catch callbacks that are not awaited/returned\nconst bad = /(^|[^.\\w])(return\\s+)?\\w+\\.(then|catch)\\([^)]*=>\\s*\\{?\\s*expect\\(/;\nif (bad.test(sourceOfTestFile)) console.warn('assertion inside a floating promise chain');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer `await expect(p).resolves.toEqual(v)` over any `.then(() => expect(...))` wrapper.","If you must chain, always `await` or `return` the whole chain from the test.","Enable no-floating-promises (TS) alongside valid-expect to catch the same class of bug for non-assertion code."],"tags":["jest","vitest","oxlint","testing","async","promise-chain","floating-promise","valid-expect"],"backgroundTag":"async-assertion-not-awaited","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"}