1 | 'use strict';
|
2 |
|
3 | const { getDocsUrl } = require('./util');
|
4 |
|
5 | module.exports = {
|
6 | meta: {
|
7 | docs: {
|
8 | url: getDocsUrl(__filename),
|
9 | },
|
10 | },
|
11 | schema: [
|
12 | {
|
13 | type: 'object',
|
14 | properties: {
|
15 | allow: {
|
16 | type: 'array',
|
17 | contains: ['beforeAll', 'beforeEach', 'afterAll', 'afterEach'],
|
18 | },
|
19 | },
|
20 | additionalProperties: false,
|
21 | },
|
22 | ],
|
23 | create(context) {
|
24 | const testHookNames = Object.assign(Object.create(null), {
|
25 | beforeAll: true,
|
26 | beforeEach: true,
|
27 | afterAll: true,
|
28 | afterEach: true,
|
29 | });
|
30 |
|
31 | const whitelistedHookNames = (
|
32 | context.options[0] || { allow: [] }
|
33 | ).allow.reduce((hashMap, value) => {
|
34 | hashMap[value] = true;
|
35 | return hashMap;
|
36 | }, Object.create(null));
|
37 |
|
38 | const isHook = node => testHookNames[node.callee.name];
|
39 | const isWhitelisted = node => whitelistedHookNames[node.callee.name];
|
40 |
|
41 | return {
|
42 | CallExpression(node) {
|
43 | if (isHook(node) && !isWhitelisted(node)) {
|
44 | context.report({
|
45 | node,
|
46 | message: "Unexpected '{{ hookName }}' hook",
|
47 | data: { hookName: node.callee.name },
|
48 | });
|
49 | }
|
50 | },
|
51 | };
|
52 | },
|
53 | };
|