# vitest-mock-at-module-scope

Rule `sonarjs/vitest-mock-at-module-scope`. Vi.mock should be declared at module scope

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1384 by reach, #685 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/vitest-mock-at-module-scope` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-9169/) |

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/vitest-mock-at-module-scope": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/use-type-alias](https://lintradar.dev/rules/sonarjs/use-type-alias.md): Type aliases should be used
- [sonarjs/void-use](https://lintradar.dev/rules/sonarjs/void-use.md): "void" should not be used
- [sonarjs/updated-loop-counter](https://lintradar.dev/rules/sonarjs/updated-loop-counter.md): Loop counters should not be assigned within the loop body
- [sonarjs/weak-ssl](https://lintradar.dev/rules/sonarjs/weak-ssl.md): Weak SSL/TLS protocols should not be used
- [sonarjs/updated-const-var](https://lintradar.dev/rules/sonarjs/updated-const-var.md): "const" variables should not be reassigned
- [sonarjs/x-powered-by](https://lintradar.dev/rules/sonarjs/x-powered-by.md): Web application technologies should not disclose version information

---

Web page: https://lintradar.dev/rules/sonarjs/vitest-mock-at-module-scope. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
