no-mocks-import
Ast Rule: import
no-mocks-import
const MOCK_FOLDER = "__mocks__"
function visit(node, filename, code) {
// only run on .spec. or .test. files
if (!filename.includes(".spec.") && !filename.includes(".test.")) return;
if (!node.pkg?.value) return;
if (!node.pkg.value.includes(MOCK_FOLDER)) return;
const error = buildError(
node.start.line,
node.start.col,
node.end.line,
node.end.col,
`Disallow manually importing from '__mocks__'`,
"WARNING",
"BEST_PRACTICE"
)
addError(error)
}
good-mock-import.spec.js
Expected test result: no error
Importing the module and not the mocks directly
bad-mock-import.test.js
Expected test result: has error
Importing mocks directly