{"record":{"id":"0480c65e868e0452","repo":"oxc-project/oxc","slug":"every-test-should-have-either-prefix-assertions","errorCode":null,"errorMessage":"Every test should have either `{prefix}.assertions(<number of assertions>)` or `{prefix}.hasAssertions()` as its first expression.","messagePattern":"Every test should have either `(.+?)\\.assertions\\(<number of assertions>\\)` or `(.+?)\\.hasAssertions\\(\\)` as its first expression\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/jest/prefer_expect_assertions.rs","lineNumber":22,"sourceCode":"use oxc_semantic::NodeId;\nuse oxc_span::Span;\nuse oxc_syntax::scope::ScopeId;\nuse serde::Deserialize;\nuse std::borrow::Cow;\n\nuse crate::{\n    context::LintContext,\n    fixer::RuleFix,\n    rule::{DefaultRuleConfig, Rule},\n    rules::shared::prefer_expect_assertions::{\n        CallbackBody, DOCUMENTATION, PreferExpectAssertionsConfig, PreferExpectAssertionsRuleImpl,\n        resolve_expect_local_name, should_check,\n    },\n    utils::collect_possible_jest_call_node,\n};\n\nfn have_expect_assertions(span: Span, prefix: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"Every test should have either `{prefix}.assertions(<number of assertions>)` or `{prefix}.hasAssertions()` as its first expression.\",\n    ))\n    .with_help(format!(\"Add `{prefix}.hasAssertions()` or `{prefix}.assertions(<number>)` as the first statement in the test.\"))\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, Deserialize)]\npub struct PreferExpectAssertions(Box<PreferExpectAssertionsConfig>);\n\nimpl std::ops::Deref for PreferExpectAssertions {\n    type Target = PreferExpectAssertionsConfig;\n\n    fn deref(&self) -> &Self::Target {\n        &self.0\n    }\n}\n\ndeclare_oxc_lint!(","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/jest/prefer_expect_assertions.rs#L4-L40","documentation":"This is oxlint's 'jest/prefer-expect-assertions' diagnostic. It reports a test body that does not begin with expect.assertions(<n>) or expect.hasAssertions() (the local expect alias is resolved via resolve_expect_local_name). Those guards make Jest verify that the expected number of assertions actually ran, catching async tests that silently skip their expects. The rule provides a fixer that inserts the missing first statement.","triggerScenarios":"Enable the rule (shared implementation with rules/shared/prefer_expect_assertions, honoring should_check and the CallbackBody config) and lint it('x', () => { expect(a).toBe(b); }) — a test whose first body statement is not a call to <expect-local>.assertions or <expect-local>.hasAssertions. The message interpolates the resolved expect prefix.","commonSituations":"Enabling strict Jest presets across an existing suite flags nearly every test at once. Callback/Promise tests written before expect.assertions existed are the historical reason the API exists; teams adopting it hit the rule during config migration or when copying old tests into a stricter project.","solutions":["Apply the autofix or manually add expect.hasAssertions() as the first statement of the test body.","If you know the count, use expect.assertions(2) so Jest enforces exactly two assertions.","If the rule is too noisy for your style, disable it in .oxlintrc.json (\"jest/prefer-expect-assertions\": \"off\").","Inline-suppress individual tests where the count is unknown with // oxlint-disable-next-line jest/prefer-expect-assertions."],"exampleFix":"// before\nit('maps values', () => {\n  expect(map('a')).toBe(1);\n});\n\n// after\nit('maps values', () => {\n  expect.hasAssertions();\n  expect(map('a')).toBe(1);\n});","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Start async/callback test bodies with expect.hasAssertions() (or expect.assertions(n)).","Decide team-wide whether the rule is on; if on, add the guard when writing the test, not later.","Use the rule's autofix during migration batches instead of hand-editing every test."],"tags":["lint","jest","testing","assertions","autofix","oxlint"],"backgroundTag":"jest-expect-assertions-count","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"}