Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts-migration/no-try-expect #344

Merged
merged 2 commits into from Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,9 +1,9 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-try-expect';

const ruleTester = new RuleTester({
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 2019,
ecmaVersion: 9,
},
});

Expand All @@ -17,13 +17,13 @@ ruleTester.run('no-try-catch', rule, {
});
try {

} catch {
} catch(e) {
expect('foo').toEqual('foo');
}`,
`it.skip('foo');
try {

} catch {
} catch(e) {
expect('foo').toEqual('foo');
}`,
],
Expand Down
20 changes: 14 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, 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,18 @@ 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 && 'name' in node.callee && node.callee.name === 'expect'
SimenB marked this conversation as resolved.
Show resolved Hide resolved
);
}

return {
Expand Down Expand Up @@ -50,4 +58,4 @@ export default {
},
};
},
};
});