no-test-prefixes
Ast Rule: function call
no-test-prefixes
const TEST_FUNCTIONS_MAP = {
xdescribe: "describe.skip",
xtest: "test.skip",
xit: "it.skip",
fdescribe: "describe.only",
ftest: "test.only",
fit: "it.only",
};
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
const testFunctionName = testFunction?.value;
const testFunctionNameReplacer = TEST_FUNCTIONS_MAP[testFunctionName];
if (!testFunctionNameReplacer) return;
const error = buildError(
testFunction.start.line,
testFunction.start.col,
testFunction.end.line,
testFunction.end.col,
"Require using '.only' and '.skip' over 'f' and 'x'",
"WARNING",
"BEST_PRACTICE"
);
const edit = buildEdit(
testFunction.start.line,
testFunction.start.col,
testFunction.end.line,
testFunction.end.col,
"update",
testFunctionNameReplacer
);
const fix = buildFix(`Use "${testFunctionNameReplacer}" instead`, [edit]);
addError(error.addFix(fix));
}
skip-and-only.test.js
Expected test result: no error
Tests using .skip and .only
f-and-x.spec.js
Expected test result: has error