/
require-top-level-describe.ts
50 lines (46 loc) · 1.27 KB
/
require-top-level-describe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { createRule, isDescribe, isHook, isTestCase } from './utils';
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description:
'Prevents test cases and hooks to be outside of a describe block',
recommended: false,
},
messages: {
unexpectedTestCase: 'All test cases must be wrapped in a describe block.',
unexpectedHook: 'All hooks must be wrapped in a describe block.',
},
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
let numberOfDescribeBlocks = 0;
return {
CallExpression(node) {
if (isDescribe(node)) {
numberOfDescribeBlocks++;
return;
}
if (numberOfDescribeBlocks === 0) {
if (isTestCase(node)) {
context.report({ node, messageId: 'unexpectedTestCase' });
return;
}
if (isHook(node)) {
context.report({ node, messageId: 'unexpectedHook' });
return;
}
}
},
'CallExpression:exit'(node: TSESTree.CallExpression) {
if (isDescribe(node)) {
numberOfDescribeBlocks--;
}
},
};
},
});