Skip to content

Commit 86c83f1

Browse files
G-RathSimenB
authored andcommitted
chore(no-hooks): convert to typescript (#322)
1 parent 7dbc12c commit 86c83f1

File tree

6 files changed

+110
-111
lines changed

6 files changed

+110
-111
lines changed

src/rules/__tests__/no-hooks.test.js

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { TSESLint } from '@typescript-eslint/experimental-utils';
2+
import rule from '../no-hooks';
3+
import { HookName } from '../tsUtils';
4+
5+
const ruleTester = new TSESLint.RuleTester({
6+
parserOptions: {
7+
ecmaVersion: 6,
8+
},
9+
});
10+
11+
ruleTester.run('no-hooks', rule, {
12+
valid: [
13+
'test("foo")',
14+
'describe("foo", () => { it("bar") })',
15+
'test("foo", () => { expect(subject.beforeEach()).toBe(true) })',
16+
{
17+
code: 'afterEach(() => {}); afterAll(() => {});',
18+
options: [{ allow: [HookName.afterEach, HookName.afterAll] }],
19+
},
20+
],
21+
invalid: [
22+
{
23+
code: 'beforeAll(() => {})',
24+
errors: [
25+
{ messageId: 'unexpectedHook', data: { hookName: HookName.beforeAll } },
26+
],
27+
},
28+
{
29+
code: 'beforeEach(() => {})',
30+
errors: [
31+
{
32+
messageId: 'unexpectedHook',
33+
data: { hookName: HookName.beforeEach },
34+
},
35+
],
36+
},
37+
{
38+
code: 'afterAll(() => {})',
39+
errors: [
40+
{ messageId: 'unexpectedHook', data: { hookName: HookName.afterAll } },
41+
],
42+
},
43+
{
44+
code: 'afterEach(() => {})',
45+
errors: [
46+
{ messageId: 'unexpectedHook', data: { hookName: HookName.afterEach } },
47+
],
48+
},
49+
{
50+
code: 'beforeEach(() => {}); afterEach(() => { jest.resetModules() });',
51+
options: [{ allow: [HookName.afterEach] }],
52+
errors: [
53+
{
54+
messageId: 'unexpectedHook',
55+
data: { hookName: HookName.beforeEach },
56+
},
57+
],
58+
},
59+
],
60+
});

src/rules/no-hooks.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/rules/no-hooks.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { HookName, createRule, isHook } from './tsUtils';
2+
3+
export default createRule({
4+
name: __filename,
5+
meta: {
6+
docs: {
7+
category: 'Best Practices',
8+
description: 'Disallow setup and teardown hooks',
9+
recommended: false,
10+
},
11+
messages: {
12+
unexpectedHook: "Unexpected '{{ hookName }}' hook",
13+
},
14+
schema: [
15+
{
16+
type: 'object',
17+
properties: {
18+
allow: {
19+
type: 'array',
20+
contains: ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'],
21+
},
22+
},
23+
additionalProperties: false,
24+
},
25+
],
26+
type: 'suggestion',
27+
},
28+
defaultOptions: [{ allow: [] } as { allow: readonly HookName[] }],
29+
create(context, [{ allow }]) {
30+
const whitelistedHookNames = allow.reduce((hashMap, value) => {
31+
hashMap[value] = true;
32+
return hashMap;
33+
}, Object.create(null));
34+
35+
const isWhitelisted = (node: { callee: { name: string } }) =>
36+
whitelistedHookNames[node.callee.name];
37+
38+
return {
39+
CallExpression(node) {
40+
if (isHook(node) && !isWhitelisted(node)) {
41+
context.report({
42+
node,
43+
messageId: 'unexpectedHook',
44+
data: { hookName: node.callee.name },
45+
});
46+
}
47+
},
48+
};
49+
},
50+
});

src/rules/tsUtils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ export const isFunction = (node: TSESTree.Node): node is FunctionExpression =>
9595
node.type === AST_NODE_TYPES.FunctionExpression ||
9696
node.type === AST_NODE_TYPES.ArrowFunctionExpression;
9797

98-
/* istanbul ignore next */
9998
export const isHook = (
10099
node: TSESTree.CallExpression,
101100
): node is JestFunctionCallExpressionWithIdentifierCallee<HookName> => {

src/rules/util.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ const describeAliases = new Set(['describe', 'fdescribe', 'xdescribe']);
101101

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

104-
const testHookNames = new Set([
105-
'beforeAll',
106-
'beforeEach',
107-
'afterAll',
108-
'afterEach',
109-
]);
110-
111104
export const getNodeName = node => {
112105
function joinNames(a, b) {
113106
return a && b ? `${a}.${b}` : null;
@@ -128,12 +121,6 @@ export const getNodeName = node => {
128121
return null;
129122
};
130123

131-
export const isHook = node =>
132-
node &&
133-
node.type === 'CallExpression' &&
134-
node.callee.type === 'Identifier' &&
135-
testHookNames.has(node.callee.name);
136-
137124
export const isTestCase = node =>
138125
node &&
139126
node.type === 'CallExpression' &&

0 commit comments

Comments
 (0)