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

How to mock a standalone function? #36

Closed
mebble opened this issue Apr 18, 2023 · 2 comments
Closed

How to mock a standalone function? #36

mebble opened this issue Apr 18, 2023 · 2 comments

Comments

@mebble
Copy link

mebble commented Apr 18, 2023

I want to mock a standalone function (i.e. a function that's not a method of some object) but I can't find information on that in the readme.

Here is a simplified version of what I'm trying to do:

type Callback = (x: number) => void;

const caller = (callback: Callback) => {
    callback(Math.random())
}

let mockCallback: Callback = mock<Callback>();

caller(mockCallback);            // fails when using mock without instance
caller(instance(mockCallback));  // fails when using mock with instance
caller(x => console.log(x))      // works when not using a mock

As shown above, the first two caller(...) calls that use the mock would fail. They fail with the error:

TypeError: callback is not a function
 ❯ caller src/test.test.ts:7:5
      5|
      6| const caller = (callback: Callback) => {
      7|     callback(Math.random())
       |     ^
      8| }

I want to mock a standalone function so that I can capture its arguments and do stuff with them. Could you please help me out? Thanks

@LironHazan
Copy link
Collaborator

@mebble Hey,
This project is in maintenance mode as the original upstream isn't maintained at all, so in general there's no plan of adding new features (especially functionalities which are available in jest/vitest) but there's a PR we can merge on that.

What do you think of the following?

let fn: (a: number, b: string) => number = fnmock();
when(fn(10, 'hello')).thenReturn(5);
instance(fn)(10, 'hello'); // returns 5
verify(fn(10, 'hello')).called();

I can port that and release another version

@mebble
Copy link
Author

mebble commented Apr 27, 2023

Yeah this API seems consistent with how we mock objects. You must be referring to PR #7?

I switched to vitest mocks as you mentioned. In case someone runs into this, here's how to mock and capture in vitest:

import { vi, Mock } from 'vitest';

type Callback = (x: number) => void;

const caller = (callback: Callback) => {
    callback(Math.random())
}

const mockCallback: Mock<Parameters<Callback>, ReturnType<Callback>> = vi.fn();

// inject the mock
caller(mockCallback);

// capture the mock's calls
const [randomNum] = mockCallback.mock.lastCall!

I think we can close this issue if the discussion on fnmock is in PR#7

@mebble mebble closed this as completed Apr 27, 2023
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