You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _utils = require("./utils");
var _default = (0, _utils.createRule)({
name: __filename,
meta: {
docs: {
description: 'Prefer using toThrow for exception tests',
category: 'Best Practices',
recommended: 'error'
},
deprecated: true,
replacedBy: ['no-conditional-expect'],
messages: {
noTryExpect: ['Tests should use Jests exception helpers.', 'Use "expect(() => yourFunction()).toThrow()" for synchronous tests,', 'or "await expect(yourFunction()).rejects.toThrow()" for async tests'].join(' ')
},
type: 'problem',
schema: []
},
defaultOptions: [],
create(context) {
let isTest = false;
let catchDepth = 0;
function isThrowExpectCall(node) {
return catchDepth > 0 && (0, _utils.isExpectCall)(node);
}
return {
CallExpression(node) {
if ((0, _utils.isTestCase)(node)) {
isTest = true;
} else if (isTest && isThrowExpectCall(node)) {
context.report({
messageId: 'noTryExpect',
node
});
}
},
FunctionDeclaration(node) {
const declaredVariables = context.getDeclaredVariables(node);
const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
if (testCallExpressions.length > 0) {
isTest = true;
}
},
CatchClause() {
if (isTest) {
++catchDepth;
}
},
'CatchClause:exit'() {
if (isTest) {
--catchDepth;
}
},
'CallExpression:exit'(node) {
if ((0, _utils.isTestCase)(node)) {
isTest = false;
}
},
'FunctionDeclaration:exit'(node) {
const declaredVariables = context.getDeclaredVariables(node);
const testCallExpressions = (0, _utils.getTestCallExpressionsFromDeclaredVariables)(declaredVariables);
if (testCallExpressions.length > 0) {
isTest = false;
}
}
};
}
});
exports.default = _default;