no-focused-tests
Ast Rule: function call
no-focused-tests
const TEST_FUNCTIONS_MAP = {
"describe.only": "describe",
"it.only": "it",
"test.only": "test",
"fdescribe": "describe",
"fit": "it",
};
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) return;
if (node.functionName.astType === "string") {
const name = TEST_FUNCTIONS_MAP[node.functionName.value]
if (!name) return;
const error = buildError(
node.start.line,
node.start.col,
node.end.line,
node.end.col,
`Disallow focused tests unless for debugging`,
"INFORMATIONAL",
"BEST_PRACTICE"
);
const edit = buildEdit(
node.functionName.start.line,
node.functionName.start.col,
node.functionName.end.line,
node.functionName.end.col,
"update",
name
);
const fix = buildFix(`Replace '${node.functionName.value}' with '${name}'`, [edit]);
addError(error.addFix(fix));
}
if (node.functionName.astType === "member") {
const first = node.functionName?.parent?.value
if (!first) return;
const second = node.functionName?.name?.value
if (!second) return;
const name = TEST_FUNCTIONS_MAP[`${first}.${second}`]
if (!name) return;
const error = buildError(
node.start.line,
node.start.col,
node.end.line,
node.end.col,
`Disallow focused tests unless for debugging`,
"WARNING",
"BEST_PRACTICE"
);
const edit = buildEdit(
node.functionName.start.line,
node.functionName.start.col,
node.functionName.end.line,
node.functionName.end.col,
"update",
name
);
const fix = buildFix(`Remove the .${second} focused test`, [edit]);
addError(error.addFix(fix));
}
}
good.test.js
Expected test result: no error
These aren't focused tests
bad.spec.js
Expected test result: has error
These are focused tests