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);
});
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.
I use
jest-when
quite a lot, and find it incredibly useful. However, I've struggled to get it to work when thecalledWith
parameter is itself a mock, like in the following scenario:The above test fails:
which indicates to me that
jest-when
has not picked up thatLambdaDestination
was called with the argumentmockLambda
and hence isn't returning the valuemockLambdaDestination
.However if I re-write the test to not use
jest-when
and instead check thatLambdaDestination
was called with the expected mock value, it passes:So it appears that jest is able to work out that
LambdaDestination
was called withmockLambda
, but thatjest-when
is not able to.These examples use
mock
from jest-mock-extended andmocked
from ts-jest. I could try to come up with examples that don't use these libraries if that would help.