no-identical-title

Try in Playground
jestjsUnknownInformational

0

No tags

No CWE or CVE

This rule looks at the title of every test and test suite.

It will report an error when two test suites or two test cases at the same level of a test suite have the same title.

View a similar ESLint rule, no-identical-title.

Ast Rule: function call


no-identical-title

How to write a rule
const TESTERS_MAP = {
  describe: "describe",
  test: "test",
  it: "it",
};

function visit(node, filename, code) {
  // only run on .spec. or .test. files
  if (!filename.includes(".spec.") && !filename.includes(".test.")) return;

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

  const testFunction = node.functionName;
  if (!TESTERS_MAP[testFunction?.value]) return;

  const content = node.arguments?.values[1]?.value?.content;
  if (!content) return;

  if (content.elements?.length < 2) return;

  let titles = {};

  for (let i = 0; i < content.elements.length; i++) {
    const element = content.elements[i];
    if (element.astType === "functioncall") {
      const funcName = element.functionName?.value?.replace(/^["']/g, "").replace(/["']$/g, "");
      if (TESTERS_MAP[funcName]) {
        if (element?.arguments?.values?.length === 0) return;
        const title = element.arguments.values[0]?.value?.value;
        if (!title) return;
        if (titles[title]) {
          const error = buildError(
            element.arguments.values[0].value.start.line,
            element.arguments.values[0].value.start.col,
            element.arguments.values[0].value.end.line,
            element.arguments.values[0].value.end.col,
            `Test title is used multiple times in the same testing block`,
            "WARNING",
            "BEST_PRACTICE"
          );
          addError(error);
        } else {
          titles[title] = true;
        }
      }
    }
  }
}

valid-titles.spec.js

Expected test result: no error

Valid titles for a test file

describe('foo', () => {
  it('should do foo', () => {});
  it('should do bar', () => {});

  // Has the same name as a parent test suite, which is fine
  describe('foo', () => {
    // Has the same name as a test in a parent test suite, which is fine
    it('should do foo', () => {});
    it('should work', () => {});
  });

  describe('baz', () => {
    // Has the same title as a previous test suite
    // Has the same name as a test in a sibling test suite, which is fine
    it('should work', () => {});
  });
});

identical-titles.test.js

Expected test result: has error

Two tests at the same level has the same title

describe('foo', () => {
	expect('dfdf').toBe('bar')
  it('should do bar', () => {});
  it('should do bar', () => {}); // Has the same title as the previous test

  describe('baz', () => {
		it('should do bar', () => {});
  });

  describe('baz', () => {
    // Has the same title as a previous test suite
    // ...
  });
});
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.