Skip to content

Commit 9d9336a

Browse files
KreegAndrey Nelyubin
authored andcommitted
feat(require-hook): add allowedFunctionCalls setting (#983)
Co-authored-by: Andrey Nelyubin <[email protected]>
1 parent c0a00a1 commit 9d9336a

File tree

3 files changed

+101
-7
lines changed

3 files changed

+101
-7
lines changed

docs/rules/require-hook.md

+37
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,40 @@ afterEach(() => {
148148
clearCityDatabase();
149149
});
150150
```
151+
152+
## Options
153+
154+
If there are methods that you want to call outside of hooks and tests, you can
155+
mark them as allowed using the `allowedFunctionCalls` option.
156+
157+
```json
158+
{
159+
"jest/require-hook": [
160+
"error",
161+
{
162+
"allowedFunctionCalls": ["enableAutoDestroy"]
163+
}
164+
]
165+
}
166+
```
167+
168+
Examples of **correct** code when using
169+
`{ "allowedFunctionCalls": ["enableAutoDestroy"] }` option:
170+
171+
```js
172+
/* eslint jest/require-hook: ["error", { "allowedFunctionCalls": ["enableAutoDestroy"] }] */
173+
174+
import { enableAutoDestroy, mount } from '@vue/test-utils';
175+
import { initDatabase, tearDownDatabase } from './databaseUtils';
176+
177+
enableAutoDestroy(afterEach);
178+
179+
beforeEach(initDatabase);
180+
afterEach(tearDownDatabase);
181+
182+
describe('Foo', () => {
183+
test('always returns 42', () => {
184+
expect(global.getAnswer()).toBe(42);
185+
});
186+
});
187+
```

src/rules/__tests__/require-hook.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ ruleTester.run('require-hook', rule, {
152152
});
153153
});
154154
`,
155+
{
156+
code: dedent`
157+
enableAutoDestroy(afterEach);
158+
159+
describe('some tests', () => {
160+
it('is false', () => {
161+
expect(true).toBe(true);
162+
});
163+
});
164+
`,
165+
options: [{ allowedFunctionCalls: ['enableAutoDestroy'] }],
166+
},
155167
],
156168
invalid: [
157169
{
@@ -374,6 +386,25 @@ ruleTester.run('require-hook', rule, {
374386
},
375387
],
376388
},
389+
{
390+
code: dedent`
391+
enableAutoDestroy(afterEach);
392+
393+
describe('some tests', () => {
394+
it('is false', () => {
395+
expect(true).toBe(true);
396+
});
397+
});
398+
`,
399+
options: [{ allowedFunctionCalls: ['someOtherName'] }],
400+
errors: [
401+
{
402+
messageId: 'useHook',
403+
line: 1,
404+
column: 1,
405+
},
406+
],
407+
},
377408
],
378409
});
379410

src/rules/require-hook.ts

+33-7
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ const isNullOrUndefined = (node: TSESTree.Expression): boolean => {
2727
);
2828
};
2929

30-
const shouldBeInHook = (node: TSESTree.Node): boolean => {
30+
const shouldBeInHook = (
31+
node: TSESTree.Node,
32+
allowedFunctionCalls: readonly string[] = [],
33+
): boolean => {
3134
switch (node.type) {
3235
case AST_NODE_TYPES.ExpressionStatement:
33-
return shouldBeInHook(node.expression);
36+
return shouldBeInHook(node.expression, allowedFunctionCalls);
3437
case AST_NODE_TYPES.CallExpression:
35-
return !isJestFnCall(node);
38+
return !(
39+
isJestFnCall(node) ||
40+
allowedFunctionCalls.includes(getNodeName(node) as string)
41+
);
3642
case AST_NODE_TYPES.VariableDeclaration: {
3743
if (node.kind === 'const') {
3844
return false;
@@ -48,7 +54,10 @@ const shouldBeInHook = (node: TSESTree.Node): boolean => {
4854
}
4955
};
5056

51-
export default createRule({
57+
export default createRule<
58+
[{ allowedFunctionCalls?: readonly string[] }],
59+
'useHook'
60+
>({
5261
name: __filename,
5362
meta: {
5463
docs: {
@@ -60,13 +69,30 @@ export default createRule({
6069
useHook: 'This should be done within a hook',
6170
},
6271
type: 'suggestion',
63-
schema: [],
72+
schema: [
73+
{
74+
type: 'object',
75+
properties: {
76+
allowedFunctionCalls: {
77+
type: 'array',
78+
items: { type: 'string' },
79+
},
80+
},
81+
additionalProperties: false,
82+
},
83+
],
6484
},
65-
defaultOptions: [],
85+
defaultOptions: [
86+
{
87+
allowedFunctionCalls: [],
88+
},
89+
],
6690
create(context) {
91+
const { allowedFunctionCalls } = context.options[0] ?? {};
92+
6793
const checkBlockBody = (body: TSESTree.BlockStatement['body']) => {
6894
for (const statement of body) {
69-
if (shouldBeInHook(statement)) {
95+
if (shouldBeInHook(statement, allowedFunctionCalls)) {
7096
context.report({
7197
node: statement,
7298
messageId: 'useHook',

0 commit comments

Comments
 (0)