You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Enforce limits for Mocha timeouts (`mocha/limit-timeout`)
2
+
3
+
🚫 This rule is _disabled_ in the ✅ `recommended`[config](https://github.com/lo1tuma/eslint-plugin-mocha#configs).
4
+
5
+
<!-- end auto-generated rule header -->
6
+
7
+
Mocha lets suites, tests, and hooks override their timeout with `this.timeout(...)` or chained calls such as `it(...).timeout(...)`. This rule lets you forbid timeout configuration entirely or constrain the values that are allowed.
8
+
9
+
This rule applies to:
10
+
11
+
-`this.timeout(...)` inside suite, test, and hook callbacks
12
+
-`describe(...).timeout(...)`, `it(...).timeout(...)`, and hook variants
13
+
- TDD interface equivalents such as `suite(...).timeout(...)` and `test(...).timeout(...)`
14
+
- Equivalent configured custom names
15
+
16
+
With `mode: "disallow"`, getter-style calls such as `this.timeout()` and `it(...).timeout()` are also reported. Other modes only check calls with a statically known numeric argument.
17
+
18
+
## Options
19
+
20
+
This rule supports the following options:
21
+
22
+
-`{ "mode": "disallow" }`: Reports every Mocha timeout call.
23
+
-`{ "mode": "disallowDisabled" }`: Reports timeout values that disable timeouts. In current Mocha behavior, that includes values less than or equal to `0` and values greater than or equal to `2^31 - 1`.
24
+
-`{ "mode": "max", "max": 5000 }`: Reports statically known numeric timeout values greater than `max`.
25
+
-`{ "mode": "range", "min": 1, "max": 5000 }`: Reports statically known numeric timeout values outside the inclusive range.
26
+
27
+
For `max` and `range`, unresolved dynamic values and string shorthands such as `"2s"` are ignored.
28
+
29
+
```json
30
+
{
31
+
"rules": {
32
+
"mocha/limit-timeout": ["error", {
33
+
"mode": "range",
34
+
"min": 1,
35
+
"max": 5000
36
+
}]
37
+
}
38
+
}
39
+
```
40
+
41
+
## Rule Details
42
+
43
+
Examples of incorrect code for this rule:
44
+
45
+
```js
46
+
it('works', function () {}).timeout(5000);
47
+
48
+
describe('suite', function () {
49
+
this.timeout(0);
50
+
});
51
+
52
+
beforeEach(function () {}).timeout(10_000);
53
+
```
54
+
55
+
Examples of correct code for this rule with `{ "mode": "range", "min": 1, "max": 5000 }`:
56
+
57
+
```js
58
+
it('works', function () {});
59
+
60
+
it('works', function () {}).timeout(5000);
61
+
62
+
describe('suite', function () {
63
+
this.timeout(2500);
64
+
});
65
+
```
66
+
67
+
Examples of correct code for this rule with `{ "mode": "disallowDisabled" }`:
68
+
69
+
```js
70
+
it('works', function () {}).timeout(5000);
71
+
72
+
describe('suite', function () {
73
+
this.timeout(2500);
74
+
});
75
+
```
76
+
77
+
## When Not To Use It
78
+
79
+
- If your project allows arbitrary Mocha timeout configuration.
80
+
- If your project relies heavily on dynamic or string-based timeout values and you need exhaustive checking for those forms.
0 commit comments