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 with a callback? #7

Open
Whoaa512 opened this Issue Nov 6, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@Whoaa512

Whoaa512 commented Nov 6, 2018

I've got a mock which needs to call a callback. Currently this is not possible with your library.

As a workaround I've got this:

myMock.mockImplementationOnce((...args) => {
    expect(args).toEqual([
        'putObject',
        {
            Bucket: 'attachments',
            Key: 'some-guid',
            Expires: 900,
            ContentType: 'application/pdf',
        },
        expect.any(Function),
    ])

    const cb = _.last(args)

    cb(null, 'https://signed-url')
})
@timkindberg

This comment has been minimized.

Owner

timkindberg commented Nov 7, 2018

Ok so that behavior of calling a callback in jest is only possible via mockImplementation, since there is no convenient yield method like sinon.js has. So we essentially need to add mockImplementation to jest-when.

We could add new methods that worked like this:

when(mockFn).calledWith(expectedArgs).mockImplementation(mockImplFn)
when(mockFn).calledWith(expectedArgs).mockImplementationOnce(mockImplFn)

A PR for these additions would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment