Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(require-hook): check variables are either const or declarations #959

Merged
merged 3 commits into from Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rules/require-hook.md
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
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
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