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

Unable to match when parameter itself is a mock? #80

Open
FergusMcGlynn opened this issue Aug 11, 2021 · 3 comments
Open

Unable to match when parameter itself is a mock? #80

FergusMcGlynn opened this issue Aug 11, 2021 · 3 comments

Comments

@FergusMcGlynn
Copy link

FergusMcGlynn commented Aug 11, 2021

I use jest-when quite a lot, and find it incredibly useful. However, I've struggled to get it to work when the calledWith parameter is itself a mock, like in the following scenario:

import { mock } from 'jest-mock-extended';
import { mocked } from 'ts-jest/utils';
import { when } from 'jest-when';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { LambdaDestination } from '@aws-cdk/aws-logs-destinations';

jest.mock('@aws-cdk/aws-logs-destinations');

test('jest-when matching on a mock', () => {
    const mockLambdaDestination = mock<LambdaDestination>();
    const mockLambda = mock<NodejsFunction>();
    when(mocked(LambdaDestination)).calledWith(mockLambda).mockReturnValue(mockLambdaDestination);

    const result = new LambdaDestination(mockLambda);

    expect(result).toEqual(mockLambdaDestination);
});

The above test fails:

Error: expect(received).toEqual(expected) // deep equality

Expected: undefined
Received: {"bind": [Function bind]}

which indicates to me that jest-when has not picked up that LambdaDestination was called with the argument mockLambda and hence isn't returning the value mockLambdaDestination.

However if I re-write the test to not use jest-when and instead check that LambdaDestination was called with the expected mock value, it passes:

import { mock } from 'jest-mock-extended';
import { mocked } from 'ts-jest/utils';
import { NodejsFunction } from '@aws-cdk/aws-lambda-nodejs';
import { LambdaDestination } from '@aws-cdk/aws-logs-destinations';

jest.mock('@aws-cdk/aws-logs-destinations');

test('jest matching on a mock without help from jest-when', () => {
    const mockLambdaDestination = mock<LambdaDestination>();
    const mockLambda = mock<NodejsFunction>();
    mocked(LambdaDestination).mockReturnValue(mockLambdaDestination);

    const result = new LambdaDestination(mockLambda);

    expect(result).toEqual(mockLambdaDestination);
    expect(LambdaDestination).toHaveBeenCalledWith(mockLambda);
});

So it appears that jest is able to work out that LambdaDestination was called with mockLambda, but that jest-when is not able to.

These examples use mock from jest-mock-extended and mocked from ts-jest. I could try to come up with examples that don't use these libraries if that would help.

@timkindberg
Copy link
Owner

Looks like jest-mocked-extended has some built-in support for calledWith behavior as well. Have you tried that?

@timkindberg
Copy link
Owner

It would help if you could remove the extra libraries from your example.

@joebowbeer
Copy link

Related to #89 ?

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

3 participants