max-expects
Ast Rule: function call
max-expects
function visit(node, filename, code) {
const isExpect = (element) => {
if (element.astType === "functioncall" &&
element.functionName &&
element.functionName.astType === "string" &&
element.functionName.value === "expect") {
return true;
}
if (element.astType === "functioncall" && element.functionName.astType === "member") {
return isExpect(element.functionName);
}
if (element.astType === "member") {
return isExpect(element.parent);
}
return false;
}
// your code here
if (!filename.includes(".spec.") && !filename.includes(".test.")) return;
if (node.functionName.astType !== "string") {
return;
}
if (node.functionName.value !== "test" && node.functionName.value !== "it") {
return;
}
if (!node.arguments || !node.arguments.values || node.arguments.values.length !== 2) {
return;
}
const callback = node.arguments.values.length && node.arguments.values[1];
if (
callback &&
callback.value.astType === "functionexpression" &&
callback.value.content &&
callback.value.content.elements
) {
const elements = callback.value.content.elements;
const nbExpect = elements.filter(e => isExpect(e)).length;
console.log(nbExpect);
if (nbExpect > 5) {
const error = buildError(node.functionName.start.line, node.functionName.start.col,
node.functionName.end.line, node.functionName.end.col,
"Too many expect()", "WARNING", "BEST_PRACTICE");
addError(error);
}
}
}
test.spec.js
Expected test result: no error
The following patterns are considered warnings
test.spec.js
Expected test result: has error
The following patterns are not considered warnings