no-focused-tests.js 1.79 KB
Newer Older
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
'use strict';

const { getDocsUrl } = require('./util');

const testFunctions = Object.assign(Object.create(null), {
  describe: true,
  it: true,
  test: true,
});

const matchesTestFunction = object => object && testFunctions[object.name];

const isCallToFocusedTestFunction = object =>
  object && object.name[0] === 'f' && testFunctions[object.name.substring(1)];

const isPropertyNamedOnly = property =>
  property && (property.name === 'only' || property.value === 'only');

const isCallToTestOnlyFunction = callee =>
  matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property);

module.exports = {
  meta: {
    docs: {
      url: getDocsUrl(__filename),
    },
  },
  create: context => ({
    CallExpression(node) {
      const { callee } = node;

      if (callee.type === 'MemberExpression') {
        if (
          callee.object.type === 'Identifier' &&
          isCallToFocusedTestFunction(callee.object)
        ) {
          context.report({
            message: 'Unexpected focused test.',
            node: callee.object,
          });
          return;
        }

        if (
          callee.object.type === 'MemberExpression' &&
          isCallToTestOnlyFunction(callee.object)
        ) {
          context.report({
            message: 'Unexpected focused test.',
            node: callee.object.property,
          });
          return;
        }

        if (isCallToTestOnlyFunction(callee)) {
          context.report({
            message: 'Unexpected focused test.',
            node: callee.property,
          });
          return;
        }
      }

      if (callee.type === 'Identifier' && isCallToFocusedTestFunction(callee)) {
        context.report({
          message: 'Unexpected focused test.',
          node: callee,
        });
        return;
      }
    },
  }),
};