github-pages-deploy-action/node_modules/eslint-plugin-jest/lib/rules/prefer-expect-assertions.js

125 lines
4.5 KiB
JavaScript
Raw Normal View History

2020-03-07 11:45:40 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
var _utils = require("./utils");
2020-06-27 02:01:06 +08:00
const isExpectAssertionsOrHasAssertionsCall = expression => expression.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && expression.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(expression.callee.object, 'expect') && (0, _utils.isSupportedAccessor)(expression.callee.property) && ['assertions', 'hasAssertions'].includes((0, _utils.getAccessorValue)(expression.callee.property));
2020-03-07 11:45:40 +08:00
2020-06-27 02:01:06 +08:00
const isFirstLineExprStmt = functionBody => functionBody[0] && functionBody[0].type === _experimentalUtils.AST_NODE_TYPES.ExpressionStatement;
2020-03-07 11:45:40 +08:00
2020-06-27 02:01:06 +08:00
const suggestRemovingExtraArguments = (args, extraArgsStartAt) => ({
messageId: 'suggestRemovingExtraArguments',
fix: fixer => fixer.removeRange([args[extraArgsStartAt].range[0] - Math.sign(extraArgsStartAt), args[args.length - 1].range[1]])
});
2020-03-07 11:45:40 +08:00
2020-06-27 02:01:06 +08:00
const suggestions = [['suggestAddingHasAssertions', 'expect.hasAssertions();'], ['suggestAddingAssertions', 'expect.assertions();']];
2020-03-07 11:45:40 +08:00
var _default = (0, _utils.createRule)({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Suggest using `expect.assertions()` OR `expect.hasAssertions()`',
2020-06-27 02:01:06 +08:00
recommended: false,
suggestion: true
2020-03-07 11:45:40 +08:00
},
messages: {
2020-06-27 02:01:06 +08:00
hasAssertionsTakesNoArguments: '`expect.hasAssertions` expects no arguments',
assertionsRequiresOneArgument: '`expect.assertions` excepts a single argument of type number',
assertionsRequiresNumberArgument: 'This argument should be a number',
haveExpectAssertions: 'Every test should have either `expect.assertions(<number of assertions>)` or `expect.hasAssertions()` as its first expression',
suggestAddingHasAssertions: 'Add `expect.hasAssertions()`',
suggestAddingAssertions: 'Add `expect.assertions(<number of assertions>)`',
suggestRemovingExtraArguments: 'Remove extra arguments'
2020-03-07 11:45:40 +08:00
},
type: 'suggestion',
schema: []
},
defaultOptions: [],
create(context) {
return {
'CallExpression[callee.name=/^(it|test)$/][arguments.1.body.body]'(node) {
const testFuncBody = node.arguments[1].body.body;
if (!isFirstLineExprStmt(testFuncBody)) {
context.report({
messageId: 'haveExpectAssertions',
2020-06-27 02:01:06 +08:00
node,
suggest: suggestions.map(([messageId, text]) => ({
messageId,
fix: fixer => fixer.insertTextBeforeRange([node.arguments[1].body.range[0] + 1, node.arguments[1].body.range[1]], text)
}))
2020-03-07 11:45:40 +08:00
});
return;
}
2020-06-27 02:01:06 +08:00
const testFuncFirstLine = testFuncBody[0].expression;
2020-03-07 11:45:40 +08:00
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
context.report({
messageId: 'haveExpectAssertions',
2020-06-27 02:01:06 +08:00
node,
suggest: suggestions.map(([messageId, text]) => ({
messageId,
fix: fixer => fixer.insertTextBefore(testFuncBody[0], text)
}))
});
return;
}
if ((0, _utils.isSupportedAccessor)(testFuncFirstLine.callee.property, 'hasAssertions')) {
if (testFuncFirstLine.arguments.length) {
context.report({
messageId: 'hasAssertionsTakesNoArguments',
node: testFuncFirstLine.callee.property,
suggest: [suggestRemovingExtraArguments(testFuncFirstLine.arguments, 0)]
});
}
return;
}
if (!(0, _utils.hasOnlyOneArgument)(testFuncFirstLine)) {
let {
loc
} = testFuncFirstLine.callee.property;
const suggest = [];
if (testFuncFirstLine.arguments.length) {
loc = testFuncFirstLine.arguments[1].loc;
suggest.push(suggestRemovingExtraArguments(testFuncFirstLine.arguments, 1));
}
context.report({
messageId: 'assertionsRequiresOneArgument',
suggest,
loc
2020-03-07 11:45:40 +08:00
});
2020-06-27 02:01:06 +08:00
return;
}
const [arg] = testFuncFirstLine.arguments;
if (arg.type === _experimentalUtils.AST_NODE_TYPES.Literal && typeof arg.value === 'number' && Number.isInteger(arg.value)) {
return;
2020-03-07 11:45:40 +08:00
}
2020-06-27 02:01:06 +08:00
context.report({
messageId: 'assertionsRequiresNumberArgument',
node: arg
});
2020-03-07 11:45:40 +08:00
}
};
}
});
exports.default = _default;