Skip to content

Commit

Permalink
chore(no-try-expect): migrate to TS (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Jul 23, 2019
1 parent 5f544c5 commit 501de4a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
@@ -1,11 +1,14 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-try-expect';

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2019,
const ruleTester = new TSESLint.RuleTester(
// @ts-ignore: https://github.com/typescript-eslint/typescript-eslint/pull/746
{
parserOptions: {
ecmaVersion: 2019,
},
},
});
);

ruleTester.run('no-try-catch', rule, {
valid: [
Expand Down
18 changes: 12 additions & 6 deletions src/rules/no-try-expect.js → src/rules/no-try-expect.ts
@@ -1,10 +1,13 @@
import { getDocsUrl, isTestCase } from './util';
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { createRule, isExpectCall, isTestCase } from './tsUtils';

export default {
export default createRule({
name: __filename,
meta: {
docs: {
description: 'Prefer using toThrow for exception tests',
uri: getDocsUrl(__filename),
category: 'Best Practices',
recommended: false,
},
messages: {
noTryExpect: [
Expand All @@ -13,13 +16,16 @@ export default {
'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 && node.callee.name === 'expect';
function isThrowExpectCall(node: TSESTree.CallExpression) {
return catchDepth > 0 && isExpectCall(node);
}

return {
Expand Down Expand Up @@ -50,4 +56,4 @@ export default {
},
};
},
};
});
4 changes: 3 additions & 1 deletion src/rules/tsUtils.ts
Expand Up @@ -59,7 +59,9 @@ interface JestExpectNamespaceMemberExpression
*
* @return {node is JestExpectCallExpression}
*/
const isExpectCall = (node: TSESTree.Node): node is JestExpectCallExpression =>
export const isExpectCall = (
node: TSESTree.Node,
): node is JestExpectCallExpression =>
node.type === AST_NODE_TYPES.CallExpression &&
isExpectIdentifier(node.callee);

Expand Down

0 comments on commit 501de4a

Please sign in to comment.