max-expects

Try in Playground
jestjs

oscar143

Best PracticeInformational

0

jest

No CWE or CVE

Enforces a maximum number assertion calls in a test body (max-expects)

As more assertions are made, there is a possible tendency for the test to be more likely to mix multiple objectives. To avoid this, this rule reports when the maximum number of assertions is exceeded.

Rule details

This rule enforces a maximum number of expect() calls.

Ast Rule: function call


max-expects

How to write a rule
function visit(node, filename, code) {
  const isExpect = (element) => {
    if (element.astType === "functioncall" &&
      element.functionName &&
      element.functionName.astType === "string" &&
      element.functionName.value === "expect") {
      return true;
    }

    if (element.astType === "functioncall" && element.functionName.astType === "member") {
      return isExpect(element.functionName);
    }

    if (element.astType === "member") {
      return isExpect(element.parent);
    }
    return false;
  }

  // your code here
  if (!filename.includes(".spec.") && !filename.includes(".test.")) return;

  if (node.functionName.astType !== "string") {
    return;
  }

  if (node.functionName.value !== "test" && node.functionName.value !== "it") {
    return;
  }

  if (!node.arguments || !node.arguments.values || node.arguments.values.length !== 2) {
    return;
  }

  const callback = node.arguments.values.length && node.arguments.values[1];

  if (
    callback &&
    callback.value.astType === "functionexpression" &&
    callback.value.content &&
    callback.value.content.elements
  ) {
    const elements = callback.value.content.elements;
    const nbExpect = elements.filter(e => isExpect(e)).length;
    console.log(nbExpect);

    if (nbExpect > 5) {
      const error = buildError(node.functionName.start.line, node.functionName.start.col,
        node.functionName.end.line, node.functionName.end.col,
        "Too many expect()", "WARNING", "BEST_PRACTICE");
      addError(error);
    }
  }

}

test.spec.js

Expected test result: no error

The following patterns are considered warnings

test('should not pass', () => {
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
});

it('should not pass', () => {
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
});

test.spec.js

Expected test result: has error

The following patterns are not considered warnings

test('shout pass');

test('shout pass', () => {});

test.skip('shout pass', () => {});

test('should pass', function () {
  expect(true).toBeDefined();
});

test('should pass', () => {
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
  expect(true).toBeDefined();
});
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.