Skip to content

Commit

Permalink
ts-migration/prefer-expect-assertions (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Aug 11, 2019
1 parent f93e0fd commit 1833255
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../prefer-expect-assertions';

const ruleTester = new RuleTester({
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 6,
},
Expand Down
68 changes: 0 additions & 68 deletions src/rules/prefer-expect-assertions.js

This file was deleted.

89 changes: 89 additions & 0 deletions src/rules/prefer-expect-assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { createRule, getAccessorValue, isSupportedAccessor } from './tsUtils';

const isExpectAssertionsOrHasAssertionsCall = (expression: TSESTree.Node) => {
if (
expression.type !== AST_NODE_TYPES.CallExpression ||
expression.callee.type !== AST_NODE_TYPES.MemberExpression ||
!isSupportedAccessor(expression.callee.object, 'expect') ||
!isSupportedAccessor(expression.callee.property)
) {
return false;
}

const expectAssertionName = getAccessorValue(expression.callee.property);

if (expectAssertionName !== 'assertions') {
return expectAssertionName === 'hasAssertions';
}

const [arg] = expression.arguments;

return (
expression.arguments &&
expression.arguments.length === 1 &&
arg.type === AST_NODE_TYPES.Literal &&
typeof arg.value === 'number' &&
Number.isInteger(arg.value)
);
};

const getFunctionFirstLine = (functionBody: [TSESTree.ExpressionStatement]) =>
functionBody[0] && functionBody[0].expression;

const isFirstLineExprStmt = (
functionBody: TSESTree.Statement[],
): functionBody is [TSESTree.ExpressionStatement] =>
functionBody[0] &&
functionBody[0].type === AST_NODE_TYPES.ExpressionStatement;

interface PreferExpectAssertionsCallExpression extends TSESTree.CallExpression {
// prettier-ignore
// WEB-40105
arguments: [
TSESTree.Expression,
TSESTree.ArrowFunctionExpression & { body: TSESTree.BlockStatement }
];
}

export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description:
'Suggest using `expect.assertions()` OR `expect.hasAssertions()`',
recommended: false,
},
messages: {
haveExpectAssertions:
'Every test should have either `expect.assertions(<number of assertions>)` or `expect.hasAssertions()` as its first expression',
},
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
return {
'CallExpression[callee.name=/^(it|test)$/][arguments.1.body.body]'(
node: PreferExpectAssertionsCallExpression,
) {
const testFuncBody = node.arguments[1].body.body;

if (!isFirstLineExprStmt(testFuncBody)) {
context.report({ messageId: 'haveExpectAssertions', node });

return;
}

const testFuncFirstLine = getFunctionFirstLine(testFuncBody);
if (!isExpectAssertionsOrHasAssertionsCall(testFuncFirstLine)) {
context.report({ messageId: 'haveExpectAssertions', node });
}
},
};
},
});

0 comments on commit 1833255

Please sign in to comment.