eslint-plugin-jest
Version:
Eslint rules for Jest
85 lines (76 loc) • 2.27 kB
JavaScript
;
/*
* This implementation is adapted from eslint-plugin-jasmine.
* MIT license, Remco Haszing.
*/
const _require = require('./util'),
getDocsUrl = _require.getDocsUrl,
getNodeName = _require.getNodeName;
module.exports = {
meta: {
docs: {
url: getDocsUrl(__filename)
},
messages: {
noAssertions: 'Test has no assertions'
},
schema: [{
type: 'object',
properties: {
assertFunctionNames: {
type: 'array',
items: [{
type: 'string'
}]
}
},
additionalProperties: false
}]
},
create(context) {
const unchecked = [];
const assertFunctionNames = new Set(context.options[0] && context.options[0].assertFunctionNames ? context.options[0].assertFunctionNames : ['expect']);
return {
CallExpression(node) {
const name = getNodeName(node.callee);
if (name === 'it' || name === 'test') {
unchecked.push(node);
} else if (assertFunctionNames.has(name)) {
// Return early in case of nested `it` statements.
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = context.getAncestors()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
const ancestor = _step.value;
const index = unchecked.indexOf(ancestor);
if (index !== -1) {
unchecked.splice(index, 1);
break;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
},
'Program:exit'() {
unchecked.forEach(node => context.report({
messageId: 'noAssertions',
node
}));
}
};
}
};