no-alias-methods

Try in Playground
jestjsBest PracticeInformational

0

No tags

No CWE or CVE

This rule ensures that only the canonical name as used in the Jest documentation is used in the code. For example, several Jest methods have alias names, such as toThrow having the alias toThrowError.

This makes it easier to search for all occurrences of the method within your code and to ensure consistency among method names.

This rule triggers a warning if the alias name, rather than the canonical name, of a method is used.

View a similar ESLint rule, no-alias-methods.

Ast Rule: function call


no-alias-methods

How to write a rule
const ALIAS_MAP = {
  toBeCalled: 'toHaveBeenCalled',
  toBeCalledTimes: 'toHaveBeenCalledTimes',
  toBeCalledWith: 'toHaveBeenCalledWith',
  lastCalledWith: 'toHaveBeenLastCalledWith',
  nthCalledWith: 'toHaveBeenNthCalledWith',
  toReturn: 'toHaveReturned',
  toReturnTimes: 'toHaveReturnedTimes',
  toReturnWith: 'toHaveReturnedWith',
  lastReturnedWith: 'toHaveLastReturnedWith',
  nthReturnedWith: 'toHaveNthReturnedWith',
  toThrowError: 'toThrow',
};

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

  const parent = node?.functionName?.parent;
  const target = node?.functionName?.name;

  if (!parent || !target || node.functionName.astType !== "member") return;

  if (parent?.value?.includes("expect")) return;

  const aliasReplacer = ALIAS_MAP[target?.value];
  if (!aliasReplacer) return;

  const error = buildError(
    target.start.line,
    target.start.col,
    target.end.line,
    target.end.col,
    `Disallow alias methods`,
    "INFORMATIONAL",
    "BEST_PRACTICE"
  );

  const edit = buildEdit(
    target.start.line,
    target.start.col,
    target.end.line,
    target.end.col,
    "update",
    aliasReplacer
  );

  const fix = buildFix(
    `Replace '${target?.value}' with its canonical name of '${aliasReplacer}'`,
    [edit]
  );

  addError(error.addFix(fix));
}

bad.spec.js

Expected test result: has error

expect(a).toBeCalled();
expect(a).toBeCalledTimes();
expect(a).toBeCalledWith();
expect(a).lastCalledWith();
expect(a).nthCalledWith();
expect(a).toReturn();
expect(a).toReturnTimes();
expect(a).toReturnWith();
expect(a).lastReturnedWith();
expect(a).nthReturnedWith();
expect(a).toThrowError();

good.test.js

Expected test result: no error

expect(a).toHaveBeenCalled();
expect(a).toHaveBeenCalledTimes();
expect(a).toHaveBeenCalledWith();
expect(a).toHaveBeenLastCalledWith();
expect(a).toHaveBeenNthCalledWith();
expect(a).toHaveReturned();
expect(a).toHaveReturnedTimes();
expect(a).toHaveReturnedWith();
expect(a).toHaveLastReturnedWith();
expect(a).toHaveNthReturnedWith();
expect(a).toThrow();
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.