webhook/node_modules/eslint-plugin-jest/lib/rules/consistent-test-it.js

102 lines
3.7 KiB
JavaScript
Raw Normal View History

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
2022-11-10 11:43:16 +01:00
var _utils = require("@typescript-eslint/utils");
var _utils2 = require("./utils");
const buildFixer = (callee, nodeName, preferredTestKeyword) => fixer => [fixer.replaceText(callee.type === _utils.AST_NODE_TYPES.MemberExpression ? callee.object : callee, getPreferredNodeName(nodeName, preferredTestKeyword))];
var _default = (0, _utils2.createRule)({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
2022-11-10 11:43:16 +01:00
description: 'Enforce `test` and `it` usage conventions',
recommended: false
},
fixable: 'code',
messages: {
consistentMethod: "Prefer using '{{ testKeyword }}' instead of '{{ oppositeTestKeyword }}'",
consistentMethodWithinDescribe: "Prefer using '{{ testKeywordWithinDescribe }}' instead of '{{ oppositeTestKeyword }}' within describe"
},
schema: [{
type: 'object',
properties: {
fn: {
2022-11-10 11:43:16 +01:00
enum: [_utils2.TestCaseName.it, _utils2.TestCaseName.test]
},
withinDescribe: {
2022-11-10 11:43:16 +01:00
enum: [_utils2.TestCaseName.it, _utils2.TestCaseName.test]
}
},
additionalProperties: false
}],
type: 'suggestion'
},
defaultOptions: [{
2022-11-10 11:43:16 +01:00
fn: _utils2.TestCaseName.test,
withinDescribe: _utils2.TestCaseName.it
}],
create(context) {
const configObj = context.options[0] || {};
2022-11-10 11:43:16 +01:00
const testKeyword = configObj.fn || _utils2.TestCaseName.test;
const testKeywordWithinDescribe = configObj.withinDescribe || configObj.fn || _utils2.TestCaseName.it;
let describeNestingLevel = 0;
return {
CallExpression(node) {
2022-11-10 11:43:16 +01:00
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
if (!jestFnCall) {
return;
}
2022-11-10 11:43:16 +01:00
if (jestFnCall.type === 'describe') {
describeNestingLevel++;
2022-11-10 11:43:16 +01:00
return;
}
2022-11-10 11:43:16 +01:00
const funcNode = node.callee.type === _utils.AST_NODE_TYPES.TaggedTemplateExpression ? node.callee.tag : node.callee.type === _utils.AST_NODE_TYPES.CallExpression ? node.callee.callee : node.callee;
if (jestFnCall.type === 'test' && describeNestingLevel === 0 && !jestFnCall.name.endsWith(testKeyword)) {
const oppositeTestKeyword = getOppositeTestKeyword(testKeyword);
context.report({
messageId: 'consistentMethod',
node: node.callee,
data: {
testKeyword,
oppositeTestKeyword
},
2022-11-10 11:43:16 +01:00
fix: buildFixer(funcNode, jestFnCall.name, testKeyword)
});
}
2022-11-10 11:43:16 +01:00
if (jestFnCall.type === 'test' && describeNestingLevel > 0 && !jestFnCall.name.endsWith(testKeywordWithinDescribe)) {
const oppositeTestKeyword = getOppositeTestKeyword(testKeywordWithinDescribe);
context.report({
messageId: 'consistentMethodWithinDescribe',
node: node.callee,
data: {
testKeywordWithinDescribe,
oppositeTestKeyword
},
2022-11-10 11:43:16 +01:00
fix: buildFixer(funcNode, jestFnCall.name, testKeywordWithinDescribe)
});
}
},
'CallExpression:exit'(node) {
2022-11-10 11:43:16 +01:00
if ((0, _utils2.isTypeOfJestFnCall)(node, context, ['describe'])) {
describeNestingLevel--;
}
}
};
}
});
exports.default = _default;
function getPreferredNodeName(nodeName, preferredTestKeyword) {
2022-11-10 11:43:16 +01:00
if (nodeName === _utils2.TestCaseName.fit) {
return 'test.only';
}
return nodeName.startsWith('f') || nodeName.startsWith('x') ? nodeName.charAt(0) + preferredTestKeyword : preferredTestKeyword;
}
function getOppositeTestKeyword(test) {
2022-11-10 11:43:16 +01:00
if (test === _utils2.TestCaseName.test) {
return _utils2.TestCaseName.it;
}
2022-11-10 11:43:16 +01:00
return _utils2.TestCaseName.test;
}