Skip to content

Commit 1e4af02

Browse files
committed
Rename no-top-level-hooks to no-root-hooks
Mocha calls hooks declared outside a suite root hooks. Rename the rule so the public rule id and documentation use Mocha's terminology. BREAKING CHANGE: Users must replace mocha/no-top-level-hooks with mocha/no-root-hooks in ESLint configs.
1 parent c53c4ed commit 1e4af02

5 files changed

Lines changed: 17 additions & 16 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ For maintainers: the rules table below is generated, and the headers in `documen
135135
| [no-pending-tests](documentation/rules/no-pending-tests.md) | Disallow pending tests | || | | 💡 |
136136
| [no-return-and-callback](documentation/rules/no-return-and-callback.md) | Disallow returning in a test or hook function that uses a callback || | | | |
137137
| [no-return-from-async](documentation/rules/no-return-from-async.md) | Disallow returning from an async test or hook | | || | |
138+
| [no-root-hooks](documentation/rules/no-root-hooks.md) | Disallow root hooks | || | | |
138139
| [no-setup-in-describe](documentation/rules/no-setup-in-describe.md) | Disallow setup in describe blocks || | | | |
139140
| [no-sibling-hooks](documentation/rules/no-sibling-hooks.md) | Disallow duplicate uses of a hook at the same level inside a suite || | | | |
140141
| [no-synchronous-tests](documentation/rules/no-synchronous-tests.md) | Disallow synchronous tests | | || | |
141-
| [no-top-level-hooks](documentation/rules/no-top-level-hooks.md) | Disallow top-level hooks | || | | |
142142
| [prefer-arrow-callback](documentation/rules/prefer-arrow-callback.md) | Require using arrow functions for callbacks | | || 🔧 | |
143143
| [valid-suite-title](documentation/rules/valid-suite-title.md) | Require suite descriptions to match a pre-configured regular expression | | || | |
144144
| [valid-test-title](documentation/rules/valid-test-title.md) | Require test descriptions to match a pre-configured regular expression | | || | |
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# Disallow top-level hooks (`mocha/no-top-level-hooks`)
1+
# Disallow root hooks (`mocha/no-root-hooks`)
22

33
⚠️ This rule _warns_ in the ✅ `recommended` [config](https://github.com/lo1tuma/eslint-plugin-mocha#configs).
44

55
<!-- end auto-generated rule header -->
66

7-
Mocha proposes hooks that allow code to be run before or after every or all tests. This helps define a common setup or teardown process for every test.
8-
These hooks should only be declared inside test suites, as they would otherwise be run before or after every test or test suite of the project, even if the test suite of the file they were declared in was skipped. This can lead to very confusing and unwanted effects.
7+
Mocha calls hooks declared outside a suite [root hooks](https://mochajs.org/features/hooks/#root-level-hooks). They run before or after tests in the root suite, which can be surprising when a file's own suites are skipped.
8+
9+
This rule disallows root hooks and requires hooks to be declared inside a suite.
910

1011
## Rule Details
1112

12-
This rule looks for every call to `before`, `after`, `beforeEach` and `afterEach` that are not in a test suite.
13+
This rule looks for every call to `before`, `after`, `beforeEach` and `afterEach` that is not in a test suite.
1314

1415
The following patterns are considered warnings:
1516

source/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import { noNestedTestsRule } from './rules/no-nested-tests.js';
1818
import { noPendingTestsRule } from './rules/no-pending-tests.js';
1919
import { noReturnAndCallbackRule } from './rules/no-return-and-callback.js';
2020
import { noReturnFromAsyncRule } from './rules/no-return-from-async.js';
21+
import { noRootHooksRule } from './rules/no-root-hooks.js';
2122
import { noSetupInDescribeRule } from './rules/no-setup-in-describe.js';
2223
import { noSiblingHooksRule } from './rules/no-sibling-hooks.js';
2324
import { noSynchronousTestsRule } from './rules/no-synchronous-tests.js';
24-
import { noTopLevelHooksRule } from './rules/no-top-level-hooks.js';
2525
import { preferArrowCallbackRule } from './rules/prefer-arrow-callback.js';
2626
import { validSuiteTitleRule } from './rules/valid-suite-title.js';
2727
import { validTestTitleRule } from './rules/valid-test-title.js';
@@ -46,7 +46,7 @@ const allRules: Linter.RulesRecord = {
4646
'mocha/no-setup-in-describe': 'error',
4747
'mocha/no-sibling-hooks': 'error',
4848
'mocha/no-synchronous-tests': 'error',
49-
'mocha/no-top-level-hooks': 'error',
49+
'mocha/no-root-hooks': 'error',
5050
'mocha/prefer-arrow-callback': 'error',
5151
'mocha/consistent-spacing-between-blocks': 'error',
5252
'mocha/consistent-interface': ['error', { interface: 'BDD' }],
@@ -73,7 +73,7 @@ const recommendedRules: Linter.RulesRecord = {
7373
'mocha/no-setup-in-describe': 'error',
7474
'mocha/no-sibling-hooks': 'error',
7575
'mocha/no-synchronous-tests': 'off',
76-
'mocha/no-top-level-hooks': 'warn',
76+
'mocha/no-root-hooks': 'warn',
7777
'mocha/prefer-arrow-callback': 'off',
7878
'mocha/valid-suite-title': 'off',
7979
'mocha/valid-test-title': 'off',
@@ -100,7 +100,7 @@ const rules = {
100100
'no-setup-in-describe': noSetupInDescribeRule,
101101
'no-sibling-hooks': noSiblingHooksRule,
102102
'no-synchronous-tests': noSynchronousTestsRule,
103-
'no-top-level-hooks': noTopLevelHooksRule,
103+
'no-root-hooks': noRootHooksRule,
104104
'prefer-arrow-callback': preferArrowCallbackRule,
105105
'consistent-spacing-between-blocks': consistentSpacingBetweenBlocksRule,
106106
'consistent-interface': consistentInterfaceRule,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { RuleTester } from 'eslint';
2-
import { noTopLevelHooksRule } from './no-top-level-hooks.js';
2+
import { noRootHooksRule } from './no-root-hooks.js';
33

44
const ruleTester = new RuleTester({ languageOptions: { sourceType: 'script' } });
55

6-
ruleTester.run('no-top-level-hooks', noTopLevelHooksRule, {
6+
ruleTester.run('no-root-hooks', noRootHooksRule, {
77
valid: [
88
'describe(function() { before(function() {}); });',
99
'describe(function() { after(function() {}); });',
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import type { Rule } from 'eslint';
22
import { createMochaVisitors } from '../ast/mocha-visitors.js';
33

4-
export const noTopLevelHooksRule: Readonly<Rule.RuleModule> = {
4+
export const noRootHooksRule: Readonly<Rule.RuleModule> = {
55
meta: {
66
type: 'problem',
77
languages: ['js/js'],
88
docs: {
9-
description: 'Disallow top-level hooks',
10-
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/no-top-level-hooks.md'
9+
description: 'Disallow root hooks',
10+
url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/documentation/rules/no-root-hooks.md'
1111
},
1212
messages: {
13-
unexpectedTopLevelHook: 'Unexpected use of Mocha `{{name}}` hook outside of a test suite'
13+
unexpectedRootHook: 'Unexpected use of Mocha `{{name}}` hook outside of a test suite'
1414
},
1515
schema: []
1616
},
@@ -24,7 +24,7 @@ export const noTopLevelHooksRule: Readonly<Rule.RuleModule> = {
2424

2525
context.report({
2626
node,
27-
messageId: 'unexpectedTopLevelHook',
27+
messageId: 'unexpectedRootHook',
2828
data: { name: visitorContext.name }
2929
});
3030
}

0 commit comments

Comments
 (0)