no-identical-title
Ast Rule: function call
no-identical-title
const TESTERS_MAP = {
describe: "describe",
test: "test",
it: "it",
};
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;
const testFunction = node.functionName;
if (!TESTERS_MAP[testFunction?.value]) return;
const content = node.arguments?.values[1]?.value?.content;
if (!content) return;
if (content.elements?.length < 2) return;
let titles = {};
for (let i = 0; i < content.elements.length; i++) {
const element = content.elements[i];
if (element.astType === "functioncall") {
const funcName = element.functionName?.value?.replace(/^["']/g, "").replace(/["']$/g, "");
if (TESTERS_MAP[funcName]) {
if (element?.arguments?.values?.length === 0) return;
const title = element.arguments.values[0]?.value?.value;
if (!title) return;
if (titles[title]) {
const error = buildError(
element.arguments.values[0].value.start.line,
element.arguments.values[0].value.start.col,
element.arguments.values[0].value.end.line,
element.arguments.values[0].value.end.col,
`Test title is used multiple times in the same testing block`,
"WARNING",
"BEST_PRACTICE"
);
addError(error);
} else {
titles[title] = true;
}
}
}
}
}
valid-titles.spec.js
Expected test result: no error
Valid titles for a test file
identical-titles.test.js
Expected test result: has error
Two tests at the same level has the same title