no-alias-methods
Ast Rule: function call
no-alias-methods
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
good.test.js
Expected test result: no error