no-dom-require
Ast Rule: assignment
no-dom-require
function cleanName(name) {
return name.substr(1).slice(0, -1);
}
function getFirstArgumentValue(arguments) {
if (arguments.values.length) {
const first = arguments.values[0];
return cleanName(first.value.value);
}
return "";
}
function visit(node, filename, code) {
// only run inside spec files
if (!filename.includes(".spec.") && !filename.includes(".test.")) return;
if (
node.right &&
node.right.astType === "functioncall" &&
node.right.functionName.astType === "string" &&
node.right.functionName.value === "require" &&
(
getFirstArgumentValue(node.right.arguments) === "dom-testing-library" ||
getFirstArgumentValue(node.right.arguments) === "@testing-library/dom"
)
) {
const error = buildError(
node.start.line,
node.start.col,
node.end.line,
node.end.col,
"import from DOM Testing Library is restricted, import from corresponding Testing Library framework instead.",
"INFORMATIONAL",
"BEST_PRACTICE"
);
addError(error);
}
}
test.spec.js
Expected test result: has error
Examples of incorrect code for this rule:
test.spec.js
Expected test result: no error
Examples of correct code for this rule