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

@golevelup/ts-jest default mocks return an empty object which is inconsistent with jest.fn() which returns undefined. #700

Open
billoneil opened this issue Feb 23, 2024 · 1 comment
Labels

Comments

@billoneil
Copy link

The docs state

The list goes on with possible problems. Enter @golevelup/ts-jest's createMock utility function. This function will create a mock object for you with all sub properties mocked as jest.fn() unless otherwise provided, to allow for easy mocking later on, but more on that later.

However the behavior doesn't quite seem to match. When we mock a class with createMock the default implementations return {} instead of undefined which is what jest.fn() returns by default. However, once you trigger a jest.resetAllMocks() the mocked functions now return undefined instead of the original {}. This is an odd inconsistency and wondering if this is expected behavior or not.

    class MyClass {
        helloWorld() {
            return "hello world";
        }
    }

    it("resetMocks after first invocation", () => {
        const myClassMock = createMock<MyClass>();

        // Why does this return `{}` until we reset mocks then it returns undefined
        expect(JSON.stringify(myClassMock.helloWorld())).toBe("{}"); // Passes
        jest.resetAllMocks();
        expect(myClassMock.helloWorld()).toBeUndefined(); // Passes
    });

    it("reset mocks before invocation", () => {
        const myClassMock = createMock<MyClass>();

        // Resetting before the first invocation doesn't have this behavior
        jest.resetAllMocks();
        expect(JSON.stringify(myClassMock.helloWorld())).toBe("{}"); // Passes
        expect(myClassMock.helloWorld()).toBeUndefined(); // Fails Received: {} expected undefined
    });
Copy link

github-actions bot commented Oct 1, 2024

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant