no-focused-tests

Try in Playground
jestjsError ProneInformational

0

No tags

No CWE or CVE

Jest has a feature that allows you to focus tests by appending .only or prepending f to a test-suite or a test-case.

This feature is really helpful to debug a failing test, so you don’t have to execute all of your tests. Before committing your changes you should ensure you remove this focus feature to ensure your tests are executed on your build system.

This rule reminds you to remove .only or the f prefix from your tests.

View a similar ESLint rule, no-focused-test.

Ast Rule: function call


no-focused-tests

How to write a rule
const TEST_FUNCTIONS_MAP = {
  "describe.only": "describe",
  "it.only": "it",
  "test.only": "test",
  "fdescribe": "describe",
  "fit": "it",
};


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) return;

  if (node.functionName.astType === "string") {
    const name = TEST_FUNCTIONS_MAP[node.functionName.value]
    if (!name) return;
    const error = buildError(
      node.start.line,
      node.start.col,
      node.end.line,
      node.end.col,
      `Disallow focused tests unless for debugging`,
      "INFORMATIONAL",
      "BEST_PRACTICE"
    );
    const edit = buildEdit(
      node.functionName.start.line,
      node.functionName.start.col,
      node.functionName.end.line,
      node.functionName.end.col,
      "update",
      name
    );
    const fix = buildFix(`Replace '${node.functionName.value}' with '${name}'`, [edit]);
    addError(error.addFix(fix));
  }

  if (node.functionName.astType === "member") {
    const first = node.functionName?.parent?.value
    if (!first) return;
    const second = node.functionName?.name?.value
    if (!second) return;
    const name = TEST_FUNCTIONS_MAP[`${first}.${second}`]
    if (!name) return;
    const error = buildError(
      node.start.line,
      node.start.col,
      node.end.line,
      node.end.col,
      `Disallow focused tests unless for debugging`,
      "WARNING",
      "BEST_PRACTICE"
    );
    const edit = buildEdit(
      node.functionName.start.line,
      node.functionName.start.col,
      node.functionName.end.line,
      node.functionName.end.col,
      "update",
      name
    );
    const fix = buildFix(`Remove the .${second} focused test`, [edit]);
    addError(error.addFix(fix));
  }
}

good.test.js

Expected test result: no error

These aren't focused tests

describe.skip('bar', () => {});
it.skip('bar', () => {});
test.skip('bar', () => {});
describe('foo', () => {});
it('foo', () => {});
test('foo', () => {});

bad.spec.js

Expected test result: has error

These are focused tests

describe.only('foo', () => {});
it.only('foo', () => {});
test.only('foo', () => {});
fdescribe('foo', () => {});
fit('foo', () => {});
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.