@@ -30,6 +30,14 @@ const ConsoleClassAndVariableCode = dedent`
3030 const console = new Console();
3131` ;
3232
33+ const ServiceClassAndMethodCode = dedent `
34+ class Service {
35+ method() {}
36+ }
37+
38+ const service = new Service();
39+ ` ;
40+
3341const toThrowMatchers = [
3442 'toThrow' ,
3543 'toThrowError' ,
@@ -54,6 +62,19 @@ const validTestCases: string[] = [
5462 'expect(() => Promise.resolve().then(console.log)).not.toThrow();' ,
5563 ...toThrowMatchers . map ( matcher => `expect(console.log).not.${ matcher } ();` ) ,
5664 ...toThrowMatchers . map ( matcher => `expect(console.log).${ matcher } ();` ) ,
65+ // https://github.com/jest-community/eslint-plugin-jest/issues/1800
66+ ...[
67+ 'const parameter = jest.mocked(service.method).mock.calls[0][0];' ,
68+ 'const calls = jest.mocked(service.method).mock.calls;' ,
69+ 'const lastCall = jest.mocked(service.method).mock.calls[0];' ,
70+ 'const mockedMethod = jest.mocked(service.method); const parameter = mockedMethod.mock.calls[0][0];' ,
71+
72+ 'jest.mocked(service.method).mock;' ,
73+
74+ 'const mockProp = jest.mocked(service.method).mock;' ,
75+ 'const result = jest.mocked(service.method, true);' ,
76+ 'jest.mocked(service.method, { shallow: true });' ,
77+ ] . map ( code => [ ServiceClassAndMethodCode , code ] . join ( '\n' ) ) ,
5778] ;
5879
5980const invalidTestCases : Array < TSESLint . InvalidTestCase < MessageIds , Options > > = [
@@ -108,6 +129,52 @@ const invalidTestCases: Array<TSESLint.InvalidTestCase<MessageIds, Options>> = [
108129 } ,
109130 ] ,
110131 } ) ) ,
132+ // Ensure that accessing .mock on non-jest.mocked() results still reports errors
133+ // Note: These cases might not report errors if the base rule doesn't consider
134+ // property access as unbound method access, so we'll remove them for now
135+ // and focus on cases that should definitely report errors
136+ // Ensure that service.method as non-argument still reports errors
137+ {
138+ code : dedent `
139+ ${ ServiceClassAndMethodCode }
140+
141+ const method = service.method;
142+ jest.mocked(method);
143+ ` ,
144+ errors : [
145+ {
146+ line : 7 ,
147+ messageId : 'unboundWithoutThisAnnotation' ,
148+ } ,
149+ ] ,
150+ } ,
151+ // Ensure that regular unbound method access still reports errors
152+ {
153+ code : dedent `
154+ ${ ServiceClassAndMethodCode }
155+
156+ const method = service.method;
157+ ` ,
158+ errors : [
159+ {
160+ line : 7 ,
161+ messageId : 'unboundWithoutThisAnnotation' ,
162+ } ,
163+ ] ,
164+ } ,
165+ {
166+ code : dedent `
167+ ${ ServiceClassAndMethodCode }
168+
169+ Promise.resolve().then(service.method);
170+ ` ,
171+ errors : [
172+ {
173+ line : 7 ,
174+ messageId : 'unboundWithoutThisAnnotation' ,
175+ } ,
176+ ] ,
177+ } ,
111178 // toThrow matchers call the expected value (which is expected to be a function)
112179 ...toThrowMatchers . map ( matcher => ( {
113180 code : dedent `
@@ -196,6 +263,7 @@ const arith = {
196263${ code }
197264 ` ;
198265}
266+
199267function addContainsMethodsClassInvalid (
200268 code : string [ ] ,
201269) : Array < TSESLint . InvalidTestCase < MessageIds , Options > > {
0 commit comments