Skip to content

jest/no-standalone-expect Correctness

What it does

Prevents expect statements outside of a test or it block. An expect within a helper function (but outside of a test or it block) will not trigger this rule.

Statements like expect.hasAssertions() will NOT trigger this rule since these calls will execute if they are not in a test block.

Why is this bad?

expect statements outside of test blocks will not be executed by the Jest test runner, which means they won't actually test anything. This can lead to false confidence in test coverage and may hide bugs that would otherwise be caught by proper testing.

Examples

Examples of incorrect code for this rule:

javascript
describe("a test", () => {
  expect(1).toBe(1);
});

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny jest/no-standalone-expect --jest-plugin
json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-standalone-expect": "error"
  }
}

References

Released under the MIT License.