1 | 'use strict';
|
2 |
|
3 | const { getAdditionalTestFunctions } = require('../util/settings');
|
4 | const astUtils = require('../util/ast');
|
5 |
|
6 | module.exports = function (context) {
|
7 | let mochaTestFunctions = [
|
8 | 'it',
|
9 | 'describe',
|
10 | 'suite',
|
11 | 'test',
|
12 | 'context',
|
13 | 'specify'
|
14 | ];
|
15 | const settings = context.settings;
|
16 | const additionalTestFunctions = getAdditionalTestFunctions(settings);
|
17 |
|
18 | mochaTestFunctions = mochaTestFunctions.concat(additionalTestFunctions);
|
19 |
|
20 | function matchesMochaTestFunction(object) {
|
21 | const name = astUtils.getNodeName(object);
|
22 |
|
23 | return mochaTestFunctions.indexOf(name) !== -1;
|
24 | }
|
25 |
|
26 | function isPropertyNamedOnly(property) {
|
27 | return property && astUtils.getPropertyName(property) === 'only';
|
28 | }
|
29 |
|
30 | function isCallToMochasOnlyFunction(callee) {
|
31 | return callee.type === 'MemberExpression' &&
|
32 | matchesMochaTestFunction(callee.object) &&
|
33 | isPropertyNamedOnly(callee.property);
|
34 | }
|
35 |
|
36 | return {
|
37 | CallExpression(node) {
|
38 | const callee = node.callee;
|
39 |
|
40 | if (callee && isCallToMochasOnlyFunction(callee)) {
|
41 | context.report({
|
42 | node: callee.property,
|
43 | message: 'Unexpected exclusive mocha test.'
|
44 | });
|
45 | }
|
46 | }
|
47 | };
|
48 | };
|
49 |
|
50 | module.exports.schema = [
|
51 | {
|
52 | type: 'object',
|
53 | properties: {
|
54 | additionalTestFunctions: {
|
55 | type: 'array',
|
56 | items: {
|
57 | type: 'string'
|
58 | },
|
59 | minItems: 1,
|
60 | uniqueItems: true
|
61 | }
|
62 | }
|
63 | }
|
64 | ];
|