Skip to content

Commit

Permalink
fix(require-hook): added optional settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Nelyubin committed Nov 14, 2021
1 parent 7833de4 commit 8405f54
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 7 deletions.
71 changes: 71 additions & 0 deletions docs/rules/require-hook.md
Expand Up @@ -148,3 +148,74 @@ afterEach(() => {
clearCityDatabase();
});
```

## Options

Some test utils provides methods which takes hook as an argument
and should be executed outside a hook.

For example https://vue-test-utils.vuejs.org/api/#enableautodestroy-hook
which takes the hook as an argument. To exclude them you can update settings

```json
{
"jest/require-hook": [
"error",
{
"excludedFunctions": [
"enableAutoDestroy"
]
}
]
}
```

Examples of **incorrect** code for the `{ "excludedFunctions": ["enableAutoDestroy"] }`
option:

```js
/* eslint jest/require-hook: ["error", { "excludedFunctions": ["enableAutoDestroy"] }] */

import {
enableAutoDestroy,
resetAutoDestroyState,
mount
} from '@vue/test-utils';
import initDatabase from './initDatabase';

enableAutoDestroy(afterEach);
initDatabase(); // this will throw a linting error

describe('Foo', () => {
test('always returns 42', () => {
expect(global.getAnswer()).toBe(42);
})
})
```


Examples of **correct** code for the `{ "excludedFunctions": ["enableAutoDestroy"] }`
option:

```js
/* eslint jest/require-hook: ["error", { "excludedFunctions": ["enableAutoDestroy"] }] */

import {
enableAutoDestroy,
resetAutoDestroyState,
mount
} from '@vue/test-utils';
import {initDatabase, tearDownDatabase} from './databaseUtils';

enableAutoDestroy(afterEach);
afterAll(resetAutoDestroyState);

beforeEach(initDatabase);
afterEach(tearDownDatabase);

describe('Foo', () => {
test('always returns 42', () => {
expect(global.getAnswer()).toBe(42);
});
});
```
31 changes: 31 additions & 0 deletions src/rules/__tests__/require-hook.test.ts
Expand Up @@ -152,6 +152,18 @@ ruleTester.run('require-hook', rule, {
});
});
`,
{
code: dedent`
enableAutoDestroy(afterEach);
describe('some tests', () => {
it('is false', () => {
expect(true).toBe(true);
});
});
`,
options: [{ excludedFunctions: ['enableAutoDestroy'] }],
},
],
invalid: [
{
Expand Down Expand Up @@ -374,6 +386,25 @@ ruleTester.run('require-hook', rule, {
},
],
},
{
code: dedent`
enableAutoDestroy(afterEach);
describe('some tests', () => {
it('is false', () => {
expect(true).toBe(true);
});
});
`,
options: [{ excludedFunctions: ['someOtherName'] }],
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
],
},
],
});

Expand Down
51 changes: 44 additions & 7 deletions src/rules/require-hook.ts
Expand Up @@ -12,6 +12,10 @@ import {
isTestCaseCall,
} from './utils';

interface RequireHooksOptions {
excludedFunctions?: readonly string[];
}

const isJestFnCall = (node: TSESTree.CallExpression): boolean => {
if (isDescribeCall(node) || isTestCaseCall(node) || isHook(node)) {
return true;
Expand All @@ -27,12 +31,28 @@ const isNullOrUndefined = (node: TSESTree.Expression): boolean => {
);
};

const shouldBeInHook = (node: TSESTree.Node): boolean => {
const isExcludedFnCall = (
node: TSESTree.CallExpression,
options: RequireHooksOptions,
): boolean => {
const nodeName = getNodeName(node);

if (nodeName === null) {
return false;
}

return !!options.excludedFunctions?.includes(nodeName);
};

const shouldBeInHook = (
node: TSESTree.Node,
options: RequireHooksOptions,
): boolean => {
switch (node.type) {
case AST_NODE_TYPES.ExpressionStatement:
return shouldBeInHook(node.expression);
return shouldBeInHook(node.expression, options);
case AST_NODE_TYPES.CallExpression:
return !isJestFnCall(node);
return !(isJestFnCall(node) || isExcludedFnCall(node, options));
case AST_NODE_TYPES.VariableDeclaration: {
if (node.kind === 'const') {
return false;
Expand All @@ -48,7 +68,7 @@ const shouldBeInHook = (node: TSESTree.Node): boolean => {
}
};

export default createRule({
export default createRule<[RequireHooksOptions], 'useHook'>({
name: __filename,
meta: {
docs: {
Expand All @@ -60,13 +80,30 @@ export default createRule({
useHook: 'This should be done within a hook',
},
type: 'suggestion',
schema: [],
schema: [
{
type: 'object',
properties: {
excludedFunctions: {
type: 'array',
items: { type: 'string' },
},
},
additionalProperties: false,
},
],
},
defaultOptions: [],
defaultOptions: [
{
excludedFunctions: [],
},
],
create(context) {
const { excludedFunctions = [] } = context.options[0] ?? {};

const checkBlockBody = (body: TSESTree.BlockStatement['body']) => {
for (const statement of body) {
if (shouldBeInHook(statement)) {
if (shouldBeInHook(statement, { excludedFunctions })) {
context.report({
node: statement,
messageId: 'useHook',
Expand Down

0 comments on commit 8405f54

Please sign in to comment.