Skip to content

Commit

Permalink
chore(no-hooks): migrate to TS (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Jul 22, 2019
1 parent f3c654c commit 12e601a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 111 deletions.
51 changes: 0 additions & 51 deletions src/rules/__tests__/no-hooks.test.js

This file was deleted.

60 changes: 60 additions & 0 deletions src/rules/__tests__/no-hooks.test.ts
@@ -0,0 +1,60 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-hooks';
import { HookName } from '../tsUtils';

const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 6,
},
});

ruleTester.run('no-hooks', rule, {
valid: [
'test("foo")',
'describe("foo", () => { it("bar") })',
'test("foo", () => { expect(subject.beforeEach()).toBe(true) })',
{
code: 'afterEach(() => {}); afterAll(() => {});',
options: [{ allow: [HookName.afterEach, HookName.afterAll] }],
},
],
invalid: [
{
code: 'beforeAll(() => {})',
errors: [
{ messageId: 'unexpectedHook', data: { hookName: HookName.beforeAll } },
],
},
{
code: 'beforeEach(() => {})',
errors: [
{
messageId: 'unexpectedHook',
data: { hookName: HookName.beforeEach },
},
],
},
{
code: 'afterAll(() => {})',
errors: [
{ messageId: 'unexpectedHook', data: { hookName: HookName.afterAll } },
],
},
{
code: 'afterEach(() => {})',
errors: [
{ messageId: 'unexpectedHook', data: { hookName: HookName.afterEach } },
],
},
{
code: 'beforeEach(() => {}); afterEach(() => { jest.resetModules() });',
options: [{ allow: [HookName.afterEach] }],
errors: [
{
messageId: 'unexpectedHook',
data: { hookName: HookName.beforeEach },
},
],
},
],
});
46 changes: 0 additions & 46 deletions src/rules/no-hooks.js

This file was deleted.

50 changes: 50 additions & 0 deletions src/rules/no-hooks.ts
@@ -0,0 +1,50 @@
import { HookName, createRule, isHook } from './tsUtils';

export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow setup and teardown hooks',
recommended: false,
},
messages: {
unexpectedHook: "Unexpected '{{ hookName }}' hook",
},
schema: [
{
type: 'object',
properties: {
allow: {
type: 'array',
contains: ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'],
},
},
additionalProperties: false,
},
],
type: 'suggestion',
},
defaultOptions: [{ allow: [] } as { allow: readonly HookName[] }],
create(context, [{ allow }]) {
const whitelistedHookNames = allow.reduce((hashMap, value) => {
hashMap[value] = true;
return hashMap;
}, Object.create(null));

const isWhitelisted = (node: { callee: { name: string } }) =>
whitelistedHookNames[node.callee.name];

return {
CallExpression(node) {
if (isHook(node) && !isWhitelisted(node)) {
context.report({
node,
messageId: 'unexpectedHook',
data: { hookName: node.callee.name },
});
}
},
};
},
});
1 change: 0 additions & 1 deletion src/rules/tsUtils.ts
Expand Up @@ -95,7 +95,6 @@ export const isFunction = (node: TSESTree.Node): node is FunctionExpression =>
node.type === AST_NODE_TYPES.FunctionExpression ||
node.type === AST_NODE_TYPES.ArrowFunctionExpression;

/* istanbul ignore next */
export const isHook = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpressionWithIdentifierCallee<HookName> => {
Expand Down
13 changes: 0 additions & 13 deletions src/rules/util.js
Expand Up @@ -101,13 +101,6 @@ const describeAliases = new Set(['describe', 'fdescribe', 'xdescribe']);

const testCaseNames = new Set(['fit', 'it', 'test', 'xit', 'xtest']);

const testHookNames = new Set([
'beforeAll',
'beforeEach',
'afterAll',
'afterEach',
]);

export const getNodeName = node => {
function joinNames(a, b) {
return a && b ? `${a}.${b}` : null;
Expand All @@ -128,12 +121,6 @@ export const getNodeName = node => {
return null;
};

export const isHook = node =>
node &&
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
testHookNames.has(node.callee.name);

export const isTestCase = node =>
node &&
node.type === 'CallExpression' &&
Expand Down

0 comments on commit 12e601a

Please sign in to comment.