no-done-callback
Ast Rule: function call
no-done-callback
const TEST = 'test'
const HOOK = 'hook'
const FUNCTIONS_MAP = {
it: TEST,
test: TEST,
describe: TEST,
beforeAll: HOOK,
beforeEach: HOOK,
afterAll: HOOK,
afterEach: HOOK,
};
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 jestFunctionType = FUNCTIONS_MAP[testFunctionName]
if (!jestFunctionType) return;
if (!node.arguments || !node.arguments.values || node.arguments.values.length === 0) return;
// look at the first parameter in here
if (jestFunctionType === HOOK) {
if (node.arguments.values[0].value?.parameters?.values?.length > 0) {
const cb = node.arguments.values[0].value?.parameters.values[0].name
const error = buildError(
cb.start.line,
cb.start.col,
cb.end.line,
cb.end.col,
"Disallow using a callback in asynchronous tests and hooks",
"WARNING",
"BEST_PRACTICE"
);
addError(error)
}
}
// look at the second parameter in here
if (jestFunctionType === TEST) {
if (node.arguments.values[1].value?.parameters?.values?.length > 0) {
const cb = node.arguments.values[1].value?.parameters.values[0].name
const error = buildError(
cb.start.line,
cb.start.col,
cb.end.line,
cb.end.col,
"Disallow using a callback in asynchronous tests and hooks",
"WARNING",
"BEST_PRACTICE"
);
addError(error)
}
}
}
done.spec.js
Expected test result: has error
no-done.test.js
Expected test result: no error
No done callbacks