valid-title
Ast Rule: function call
valid-title
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.astType !== "string") return;
// only check describe, test and it functions
if (!TEST_FUNCTIONS.includes(node.functionName.value)) return;
if (
!node.arguments ||
!node.arguments.values ||
node.arguments.values.length !== 2
)
return;
const testFunctionName = node.functionName.value;
const testTitle = node.arguments.values[0];
if (testTitle.value.astType !== "string") return;
if (testTitle.value.value.length === 2) {
const error = buildError(
testTitle.start.line,
testTitle.start.col,
testTitle.end.line,
testTitle.end.col,
"Give your test an informative title",
"WARNING",
"BEST_PRACTICE"
);
addError(error);
} else {
const strippedTitle = testTitle.value.value
.replace(/^["']/g, "")
.replace(/["']$/g, "");
if (strippedTitle.startsWith(testFunctionName)) {
const error = buildError(
testTitle.start.line,
testTitle.start.col,
testTitle.end.line,
testTitle.end.col,
`Should not have a duplicate prefix. ex) ${testFunctionName}("${testFunctionName}...", ...)`,
"WARNING",
"BEST_PRACTICE"
);
const edit = buildEdit(
testTitle.start.line,
testTitle.start.col,
testTitle.end.line,
testTitle.end.col,
"update",
`"${strippedTitle.substring(testFunctionName.length).trim()}"`
);
const fix = buildFix("remove the duplicate prefix from title", [edit]);
addError(error.addFix(fix));
}
}
}
test.js
Expected test result: no error
Incorrect filename structure
missing-titles.spec.js
Expected test result: has error
These tests are all missing title
valid-titles.test.js
Expected test result: no error
These tests all have titles
duplicate-prefix.test.js
Expected test result: has error
These tests have duplicates the prefix in the title