1 | 'use strict';
|
2 |
|
3 | const {
|
4 | getDocsUrl,
|
5 | isDescribe,
|
6 | isTestCase,
|
7 | isString,
|
8 | hasExpressions,
|
9 | getStringValue,
|
10 | } = require('./util');
|
11 |
|
12 | const newDescribeContext = () => ({
|
13 | describeTitles: [],
|
14 | testTitles: [],
|
15 | });
|
16 |
|
17 | const handleTestCaseTitles = (context, titles, node, title) => {
|
18 | if (isTestCase(node)) {
|
19 | if (titles.indexOf(title) !== -1) {
|
20 | context.report({
|
21 | message:
|
22 | 'Test title is used multiple times in the same describe block.',
|
23 | node,
|
24 | });
|
25 | }
|
26 | titles.push(title);
|
27 | }
|
28 | };
|
29 |
|
30 | const handleDescribeBlockTitles = (context, titles, node, title) => {
|
31 | if (!isDescribe(node)) {
|
32 | return;
|
33 | }
|
34 | if (titles.indexOf(title) !== -1) {
|
35 | context.report({
|
36 | message:
|
37 | 'Describe block title is used multiple times in the same describe block.',
|
38 | node,
|
39 | });
|
40 | }
|
41 | titles.push(title);
|
42 | };
|
43 |
|
44 | const isFirstArgValid = arg => {
|
45 | if (!arg || !isString(arg)) {
|
46 | return false;
|
47 | }
|
48 | if (arg.type === 'TemplateLiteral' && hasExpressions(arg)) {
|
49 | return false;
|
50 | }
|
51 | return true;
|
52 | };
|
53 |
|
54 | module.exports = {
|
55 | meta: {
|
56 | docs: {
|
57 | url: getDocsUrl(__filename),
|
58 | },
|
59 | },
|
60 | create(context) {
|
61 | const contexts = [newDescribeContext()];
|
62 | return {
|
63 | CallExpression(node) {
|
64 | const currentLayer = contexts[contexts.length - 1];
|
65 | if (isDescribe(node)) {
|
66 | contexts.push(newDescribeContext());
|
67 | }
|
68 | const [firstArgument] = node.arguments;
|
69 | if (!isFirstArgValid(firstArgument)) {
|
70 | return;
|
71 | }
|
72 | const title = getStringValue(firstArgument);
|
73 | handleTestCaseTitles(context, currentLayer.testTitles, node, title);
|
74 | handleDescribeBlockTitles(
|
75 | context,
|
76 | currentLayer.describeTitles,
|
77 | node,
|
78 | title
|
79 | );
|
80 | },
|
81 | 'CallExpression:exit'(node) {
|
82 | if (isDescribe(node)) {
|
83 | contexts.pop();
|
84 | }
|
85 | },
|
86 | };
|
87 | },
|
88 | };
|