You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionunpackMemberExpression(node){if(node.type==='Identifier'){returnnode.name;}return`${unpackMemberExpression(node.object)}.${node.property.name}`;}functiongetFnName(node){if(node.type==='MemberExpression'){// case `Obj.myFn as jest.Mock`returnunpackMemberExpression(node);}if(node.type==='TSAsExpression'){// case: `myFn as unknown as jest.Mock`returngetFnName(node.expression);}// case `myFn as jest.Mock`returnnode.name;}constmockTypes=['MockedFunction','Mock'];// Bad:// (myFn as jest.Mock)// myFn as jest.MockedFunction<typeof myFn>// Good:// jest.mocked(myFn).mockReturnValue(...)constnoAsJestExpression={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){constisJestMock=node?.typeAnnotation?.typeName?.left?.name==='jest'&&mockTypes.includes(node?.typeAnnotation?.typeName?.right?.name);if(!isJestMock){return;}constfnName=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){returnfixer.replaceText(node,`jest.mocked(${fnName})`);},});},};},};module.exports=noAsJestExpression;
The text was updated successfully, but these errors were encountered:
Requirements:
as jest.MockedFunction
type castingas jest.Mock
type castingExamples:
Draft implementation
The text was updated successfully, but these errors were encountered: