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

Overriding functions does not work #1495

Open
hosswald opened this issue Sep 26, 2023 · 2 comments
Open

Overriding functions does not work #1495

hosswald opened this issue Sep 26, 2023 · 2 comments

Comments

@hosswald
Copy link

hosswald commented Sep 26, 2023

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

  • 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";

interface Person {
    mockedMethod(): "Foo";
    stubbedMethod(): string;
}

test("Call and spy on custom method", () => {
    const mock = createMock<Person>({
        stubbedMethod(): string {return "Bar"}
    });
    expect(mock.mockedMethod()).toBe("Foo") // ok
    expect(On(mock).get(method(mock => mock.mockedMethod))).toHaveBeenCalled() // ok
    expect(mock.stubbedMethod()).toBe("Bar") // ok
    expect(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.

const mock = createMock<Person>({
    stubbedMethod: jest.fn(()=> "Bar")
});
@hosswald
Copy link
Author

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.

@uittorio
Copy link
Member

Hello,

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants