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

mock().using does not work on method nested inside class property [@automock] #328

Open
1 task done
tsahnar opened this issue May 30, 2024 · 0 comments
Open
1 task done
Labels
bug Something isn't working hacktoberfest Hacktoberfest

Comments

@tsahnar
Copy link

tsahnar commented May 30, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

We are encountering difficulties when attempting to mock methods that are defined inside existing class properties. The nested property is automatically overwritten with a mocked object, so the actual method inside the property we need to mock is undefined and cannot be accessed (and cannot be mocked, basically).

Steps to reproduce

  1. Define a class with a property that contains a method. e.g.:
class OtherClass {
   public  getSomething: () => string
}
class MyClass {
    readonly property: OtherClass
}
  1. . Attempt to mock MyClass
let myClass:  jest.Mocked<MyClass>;

const { unit, unitRef } = TestBed.create(ServiceUnderTest)
      .mock(MyClass)
      .using({
         property: {
               getSomething: jest.fn()
          }
       })
 .compile();

myClass = unitRef.get(MyClass)
  1. In the actual test try to mock resolved value getSomething, e.g:

myClass.property.getSomething.mockResolvedValue('mocked')

Expected behavior

The method inside the class property should be successfully mocked. In the above example trying to do
.getSomething.mockResolvedValue Should not throw an error - getSomething is undefined

Automock version

v2.1.0

@omermorad omermorad added the bug Something isn't working label May 30, 2024
@omermorad omermorad changed the title mock().using does not work on method nested inside class propertry mock().using does not work on method nested inside class property [@automock] Jul 13, 2024
@omermorad omermorad added the hacktoberfest Hacktoberfest label Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hacktoberfest Hacktoberfest
Projects
None yet
Development

No branches or pull requests

3 participants