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

[new-rule] Force to use jest.mocked(fn) instead of fn as jest.Mock #1470

Open
retyui opened this issue Nov 24, 2023 · 1 comment
Open

[new-rule] Force to use jest.mocked(fn) instead of fn as jest.Mock #1470

retyui opened this issue Nov 24, 2023 · 1 comment
Labels

Comments

@retyui
Copy link

retyui commented Nov 24, 2023

Requirements:

  • ts only
  • Jest version 28+ only
  • auto-fixable
  • should cover as jest.MockedFunction type casting
  • should cover as jest.Mock type casting
  • should fix all the next examples

Examples:

// bad
const mockedUseFocused = useFocused as jest.MockedFunction<typeof useFocused>;
(AccessibilityInfo.isAccessibilityServiceEnabled as jest.Mock).mockResolvedValue(false);
const filter = (MessageService.getMessage as jest.Mock).mock.calls[0][0];
((getIsUserProfileFeatureSwitchEnabled as unknown) as jest.Mock).mockReturnValue(false);
((obj.obg2.obg3.obgN as jest.Mock).mockReturnValue(1);
((obj.myfn().myfn2 as jest.Mock).mockReturnValue('str');


// better
const mockedUseFocused = jest.mocked(useFocused)
jest.mocked(AccessibilityInfo.isAccessibilityServiceEnabled).mockResolvedValue(false);
const filter = jest.mocked(MessageService.getMessage).mock.calls[0][0];
jest.mocked(getIsUserProfileFeatureSwitchEnabled).mockReturnValue(false);
jest.mocked(obj.obg2.obg3.obgN).mockReturnValue(1);
jest.mocked(obj.myfn().myfn2).mockReturnValue('str');

Draft implementation

function unpackMemberExpression(node) {
  if (node.type === 'Identifier') {
    return node.name;
  }
  return `${unpackMemberExpression(node.object)}.${node.property.name}`;
}

function getFnName(node) {
  if (node.type === 'MemberExpression') {
    // case `Obj.myFn as jest.Mock`
    return unpackMemberExpression(node);
  }
  if (node.type === 'TSAsExpression') {
    // case: `myFn as unknown as jest.Mock`
    return getFnName(node.expression);
  }

  // case `myFn as jest.Mock`
  return node.name;
}

const mockTypes = ['MockedFunction', 'Mock'];

// Bad:
//   (myFn as jest.Mock)
//   myFn as jest.MockedFunction<typeof myFn>
// Good:
//   jest.mocked(myFn).mockReturnValue(...)
const noAsJestExpression = {
  meta: {
    type: 'suggestion',
    fixable: 'code',
    messages: {
      noAsJestExpression:
        'Please use jest api `jest.mocked({{fnName}})` instead of `{{fnName}} as jest.Mock`',
    },
  },
  create(context) {
    return {
      TSAsExpression(node) {
        const isJestMock =
          node?.typeAnnotation?.typeName?.left?.name === 'jest' &&
          mockTypes.includes(node?.typeAnnotation?.typeName?.right?.name);

        if (!isJestMock) {
          return;
        }

        const fnName = getFnName(node.expression);

        if (!fnName) {
          console.log(
            "[no-as-jest-expression]: Can't extracted fn name from expression:\n" +
              context.getSourceCode().getText(node)
          );
          return;
        }

        context.report({
          node,
          messageId: 'noAsJestExpression',
          data: {
            fnName,
          },
          fix(fixer) {
            return fixer.replaceText(node, `jest.mocked(${fnName})`);
          },
        });
      },
    };
  },
};

module.exports = noAsJestExpression;
@SimenB
Copy link
Member

SimenB commented Nov 26, 2023

I like it - happy to take a PR 👍

@jest-community jest-community deleted a comment from Badfoob Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants