Skip to content

Commit

Permalink
fix(require-hook): check variables are either const or declarations (
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Oct 17, 2021
1 parent 5278fcb commit ce8cd61
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/rules/require-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ directly within the body of a `describe`, _except_ for the following:

- `import` statements
- `const` variables
- `let` _declarations_
- `let` _declarations_, and initializations to `null` or `undefined`
- Types
- Calls to the standard Jest globals

Expand Down
107 changes: 107 additions & 0 deletions src/rules/__tests__/require-hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ ruleTester.run('require-hook', rule, {
});
});
`,
dedent`
let consoleErrorSpy = null;
beforeEach(() => {
consoleErrorSpy = jest.spyOn(console, 'error');
});
`,
dedent`
let consoleErrorSpy = undefined;
beforeEach(() => {
consoleErrorSpy = jest.spyOn(console, 'error');
});
`,
dedent`
describe('some tests', () => {
beforeEach(() => {
Expand Down Expand Up @@ -145,6 +159,27 @@ ruleTester.run('require-hook', rule, {
},
],
},
{
code: dedent`
let { setup } = require('./test-utils');
describe('some tests', () => {
setup();
});
`,
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
{
messageId: 'useHook',
line: 4,
column: 3,
},
],
},
{
code: dedent`
describe('some tests', () => {
Expand Down Expand Up @@ -176,6 +211,73 @@ ruleTester.run('require-hook', rule, {
},
],
},
{
code: dedent`
let consoleErrorSpy = jest.spyOn(console, 'error');
describe('when loading cities from the api', () => {
let consoleWarnSpy = jest.spyOn(console, 'warn');
});
`,
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
{
messageId: 'useHook',
line: 4,
column: 3,
},
],
},
{
code: dedent`
let consoleErrorSpy = null;
describe('when loading cities from the api', () => {
let consoleWarnSpy = jest.spyOn(console, 'warn');
});
`,
errors: [
{
messageId: 'useHook',
line: 4,
column: 3,
},
],
},
{
code: 'let value = 1',
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
],
},
{
code: "let consoleErrorSpy, consoleWarnSpy = jest.spyOn(console, 'error');",
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
],
},
{
code: "let consoleErrorSpy = jest.spyOn(console, 'error'), consoleWarnSpy;",
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
],
},
{
code: dedent`
import { database, isCity } from '../database';
Expand Down Expand Up @@ -236,6 +338,11 @@ ruleTester.run('require-hook', rule, {
line: 16,
column: 1,
},
{
messageId: 'useHook',
line: 31,
column: 3,
},
{
messageId: 'useHook',
line: 33,
Expand Down
17 changes: 17 additions & 0 deletions src/rules/require-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isDescribeCall,
isFunction,
isHook,
isIdentifier,
isTestCaseCall,
} from './utils';

Expand All @@ -19,12 +20,28 @@ const isJestFnCall = (node: TSESTree.CallExpression): boolean => {
return !!getNodeName(node)?.startsWith('jest.');
};

const isNullOrUndefined = (node: TSESTree.Expression): boolean => {
return (
(node.type === AST_NODE_TYPES.Literal && node.value === null) ||
isIdentifier(node, 'undefined')
);
};

const shouldBeInHook = (node: TSESTree.Node): boolean => {
switch (node.type) {
case AST_NODE_TYPES.ExpressionStatement:
return shouldBeInHook(node.expression);
case AST_NODE_TYPES.CallExpression:
return !isJestFnCall(node);
case AST_NODE_TYPES.VariableDeclaration: {
if (node.kind === 'const') {
return false;
}

return node.declarations.some(
({ init }) => init !== null && !isNullOrUndefined(init),
);
}

default:
return false;
Expand Down

0 comments on commit ce8cd61

Please sign in to comment.