eslint-plugin-jest
Version:
Eslint rules for Jest
57 lines (46 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _utils = require("./utils");
const RETURN_STATEMENT = 'ReturnStatement';
const BLOCK_STATEMENT = 'BlockStatement';
const getBody = args => {
const [, secondArg] = args;
if (secondArg && (0, _utils.isFunction)(secondArg) && secondArg.body && secondArg.body.type === BLOCK_STATEMENT) {
return secondArg.body.body;
}
return [];
};
var _default = (0, _utils.createRule)({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow explicitly returning from tests',
recommended: false
},
messages: {
noReturnValue: 'Jest tests should not return a value.'
},
schema: [],
type: 'suggestion'
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!(0, _utils.isTestCase)(node)) return;
const body = getBody(node.arguments);
const returnStmt = body.find(t => t.type === RETURN_STATEMENT);
if (!returnStmt) return;
context.report({
messageId: 'noReturnValue',
node: returnStmt
});
}
};
}
});
exports.default = _default;