1 | 'use strict';
|
2 |
|
3 | const R = require('ramda');
|
4 |
|
5 | const isDefined = R.complement(R.isNil);
|
6 | const isCallExpression = R.both(isDefined, R.propEq('type', 'CallExpression'));
|
7 |
|
8 | const describeAliases = [
|
9 | 'describe', 'xdescribe', 'describe.only', 'describe.skip',
|
10 | 'context', 'xcontext', 'context.only', 'context.skip',
|
11 | 'suite', 'xsuite', 'suite.only', 'suite.skip'
|
12 | ];
|
13 | const hooks = [ 'before', 'after', 'beforeEach', 'afterEach', 'beforeAll', 'afterAll' ];
|
14 | const testCaseNames = [
|
15 | 'it', 'it.only', 'it.skip', 'xit',
|
16 | 'test', 'test.only', 'test.skip',
|
17 | 'specify', 'specify.only', 'specify.skip', 'xspecify'
|
18 | ];
|
19 |
|
20 | function getPropertyName(property) {
|
21 | return property.name || property.value;
|
22 | }
|
23 |
|
24 | function getNodeName(node) {
|
25 | if (node.type === 'MemberExpression') {
|
26 | return `${getNodeName(node.object) }.${ getPropertyName(node.property)}`;
|
27 | }
|
28 | return node.name;
|
29 | }
|
30 |
|
31 | function isDescribe(node, additionalSuiteNames = []) {
|
32 | return isCallExpression(node) &&
|
33 | describeAliases.concat(additionalSuiteNames).indexOf(getNodeName(node.callee)) > -1;
|
34 | }
|
35 |
|
36 | function isHookIdentifier(node) {
|
37 | return node &&
|
38 | node.type === 'Identifier' &&
|
39 | hooks.indexOf(node.name) !== -1;
|
40 | }
|
41 |
|
42 | function isHookCall(node) {
|
43 | return isCallExpression(node) && isHookIdentifier(node.callee);
|
44 | }
|
45 |
|
46 | function isTestCase(node) {
|
47 | return isCallExpression(node) && testCaseNames.indexOf(getNodeName(node.callee)) > -1;
|
48 | }
|
49 |
|
50 | function findReference(scope, node) {
|
51 | const hasSameRangeAsNode = R.pathEq([ 'identifier', 'range' ], node.range);
|
52 |
|
53 | return R.find(hasSameRangeAsNode, scope.references);
|
54 | }
|
55 |
|
56 | function isShadowed(scope, identifier) {
|
57 | const reference = findReference(scope, identifier);
|
58 |
|
59 | return reference && reference.resolved && reference.resolved.defs.length > 0;
|
60 | }
|
61 |
|
62 | function isCallToShadowedReference(node, scope) {
|
63 | const identifier = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee;
|
64 |
|
65 | return isShadowed(scope, identifier);
|
66 | }
|
67 |
|
68 | function isMochaFunctionCall(node, scope) {
|
69 | if (isCallToShadowedReference(node, scope)) {
|
70 | return false;
|
71 | }
|
72 |
|
73 | return isTestCase(node) || isDescribe(node) || isHookCall(node);
|
74 | }
|
75 |
|
76 | function isStringLiteral(node) {
|
77 | return node && node.type === 'Literal' && typeof node.value === 'string';
|
78 | }
|
79 |
|
80 | function hasParentMochaFunctionCall(functionExpression) {
|
81 | return isTestCase(functionExpression.parent) || isHookCall(functionExpression.parent);
|
82 | }
|
83 |
|
84 | function isExplicitUndefined(node) {
|
85 | return node && node.type === 'Identifier' && node.name === 'undefined';
|
86 | }
|
87 |
|
88 | function isReturnOfUndefined(node) {
|
89 | const argument = node.argument;
|
90 | const isImplicitUndefined = argument === null;
|
91 |
|
92 | return isImplicitUndefined || isExplicitUndefined(argument);
|
93 | }
|
94 |
|
95 | const findReturnStatement = R.find(R.propEq('type', 'ReturnStatement'));
|
96 |
|
97 | module.exports = {
|
98 | isDescribe,
|
99 | isHookIdentifier,
|
100 | isTestCase,
|
101 | getPropertyName,
|
102 | getNodeName,
|
103 | isMochaFunctionCall,
|
104 | isHookCall,
|
105 | isStringLiteral,
|
106 | hasParentMochaFunctionCall,
|
107 | findReturnStatement,
|
108 | isReturnOfUndefined
|
109 | };
|