valid-title

Try in Playground
jestjsBest PracticeInformational

0

No tags

No CWE or CVE

This rule only runs on test files structured with a filename like the following:

  • <name>.spec.js
  • <name>.test.js

It will warn you when the title of Jest blocks are

  • empty
  • are prefixed with their block name

View a similar ESLint rule, valid-title.

Ast Rule: function call


valid-title

How to write a rule
const TEST_FUNCTIONS = ["describe", "test", "it", "xdescribe", "xtest", "xit", "fdescribe", "ftest", "fit"];

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;

  // only check describe, test and it functions
  if (!TEST_FUNCTIONS.includes(node.functionName.value)) return;

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

  const testFunctionName = node.functionName.value;
  const testTitle = node.arguments.values[0];

  if (testTitle.value.astType !== "string") return;
  if (testTitle.value.value.length === 2) {
    const error = buildError(
      testTitle.start.line,
      testTitle.start.col,
      testTitle.end.line,
      testTitle.end.col,
      "Give your test an informative title",
      "WARNING",
      "BEST_PRACTICE"
    );
    addError(error);
  } else {
    const strippedTitle = testTitle.value.value
      .replace(/^["']/g, "")
      .replace(/["']$/g, "");
    if (strippedTitle.startsWith(testFunctionName)) {
      const error = buildError(
        testTitle.start.line,
        testTitle.start.col,
        testTitle.end.line,
        testTitle.end.col,
        `Should not have a duplicate prefix. ex) ${testFunctionName}("${testFunctionName}...", ...)`,
        "WARNING",
        "BEST_PRACTICE"
      );
      const edit = buildEdit(
        testTitle.start.line,
        testTitle.start.col,
        testTitle.end.line,
        testTitle.end.col,
        "update",
        `"${strippedTitle.substring(testFunctionName.length).trim()}"`
      );
      const fix = buildFix("remove the duplicate prefix from title", [edit]);
      addError(error.addFix(fix));
    }
  }
}

test.js

Expected test result: no error

Incorrect filename structure

test("wrong filename", () => {
	expect(true).toBe(true)
})

missing-titles.spec.js

Expected test result: has error

These tests are all missing title

describe('', () => {});
describe('foo', () => {
  it('', () => {});
});
it('', () => {});
test('', () => {});
xdescribe('', () => {});
xit('', () => {});
xtest('', () => {});

valid-titles.test.js

Expected test result: no error

These tests all have titles

describe('foo', () => {});
describe('foo', () => {
  it('bar', () => {});
});
test('foo', () => {});
it('foo', () => {});
xdescribe('foo', () => {});
xit('foo', () => {});
xtest('foo', () => {});

duplicate-prefix.test.js

Expected test result: has error

These tests have duplicates the prefix in the title

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.