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
I'm trying to create a mock where some methods perform complex operations and return certain values not fixed in the interface. I also need to be able to use expectations like toHaveBeenCalled() on those methods.
Unfortunately, this does not seem to work. Methods can either be stubbed, or spied upon, not both. The documentation does not explicitly mention overriding functional properties, so I'm not sure if this is a bug or a missing feature - but for me, this restricts the usefulness of the library to a degree where I cannot use it to write the integration tests that I want to write (testing a library with a mocked dependency - I need to ensure that the library uses the dependency in certain ways, but the dependency also needs to behave in a certain way such that the library does not abort).
Your environment
ts-auto-mock version: 3.7.1
typescript version: 5.1.6
node version: 20.5.0
npm version: 9.8.0
jest-ts-auto-mock: 2.1.0
jest-extended: 4.0.1
ts-jest: 29.0.5
Steps to reproduce
Run with ts-jest:
import{createMock}from"ts-auto-mock";import{method,On,}from"ts-auto-mock/extension";interfacePerson{mockedMethod(): "Foo";stubbedMethod(): string;}test("Call and spy on custom method",()=>{constmock=createMock<Person>({stubbedMethod(): string{return"Bar"}});expect(mock.mockedMethod()).toBe("Foo")// okexpect(On(mock).get(method(mock=>mock.mockedMethod))).toHaveBeenCalled()// okexpect(mock.stubbedMethod()).toBe("Bar")// okexpect(On(mock).get(method(mock=>mock.stubbedMethod))).toHaveBeenCalled()// Matcher error: received value must be a mock or spy function})
Expected behavior
The test should succeed.
Actual behavior
Error: expect(received).toHaveBeenCalled()
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function getName]
Workaround
Wrapping the stubbed function in jest.fn() solves the problem in this case. I'm not sure if there are any unintended consequences.
I understand that the library is no longer being developed - so if this feature will not be implemented, please mention the limitation in the documentation.
Thank you for writing such a detail report. This works as expected! With overrides we never intended to persist the method mock and just update the return value. I happy to add a paragraph.
Subject of the issue
I'm trying to create a mock where some methods perform complex operations and return certain values not fixed in the interface. I also need to be able to use expectations like
toHaveBeenCalled()
on those methods.Unfortunately, this does not seem to work. Methods can either be stubbed, or spied upon, not both.
The documentation does not explicitly mention overriding functional properties, so I'm not sure if this is a bug or a missing feature - but for me, this restricts the usefulness of the library to a degree where I cannot use it to write the integration tests that I want to write (testing a library with a mocked dependency - I need to ensure that the library uses the dependency in certain ways, but the dependency also needs to behave in a certain way such that the library does not abort).
Your environment
Steps to reproduce
Run with ts-jest:
Expected behavior
The test should succeed.
Actual behavior
Workaround
Wrapping the stubbed function in
jest.fn()
solves the problem in this case. I'm not sure if there are any unintended consequences.The text was updated successfully, but these errors were encountered: