UNPKG

1.83 kBJavaScriptView Raw
1'use strict';
2
3const { getDocsUrl } = require('./util');
4
5const testFunctions = Object.assign(Object.create(null), {
6 describe: true,
7 it: true,
8 test: true,
9});
10
11const matchesTestFunction = object => object && testFunctions[object.name];
12
13const isCallToFocusedTestFunction = object =>
14 object && object.name[0] === 'f' && testFunctions[object.name.substring(1)];
15
16const isPropertyNamedOnly = property =>
17 property && (property.name === 'only' || property.value === 'only');
18
19const isCallToTestOnlyFunction = callee =>
20 matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property);
21
22module.exports = {
23 meta: {
24 docs: {
25 url: getDocsUrl(__filename),
26 },
27 },
28 create: context => ({
29 CallExpression(node) {
30 const { callee } = node;
31
32 if (callee.type === 'MemberExpression') {
33 if (
34 callee.object.type === 'Identifier' &&
35 isCallToFocusedTestFunction(callee.object)
36 ) {
37 context.report({
38 message: 'Unexpected focused test.',
39 node: callee.object,
40 });
41 return;
42 }
43
44 if (
45 callee.object.type === 'MemberExpression' &&
46 isCallToTestOnlyFunction(callee.object)
47 ) {
48 context.report({
49 message: 'Unexpected focused test.',
50 node: callee.object.property,
51 });
52 return;
53 }
54
55 if (isCallToTestOnlyFunction(callee)) {
56 context.report({
57 message: 'Unexpected focused test.',
58 node: callee.property,
59 });
60 return;
61 }
62 }
63
64 if (callee.type === 'Identifier' && isCallToFocusedTestFunction(callee)) {
65 context.report({
66 message: 'Unexpected focused test.',
67 node: callee,
68 });
69 return;
70 }
71 },
72 }),
73};