I have incorporated jest-mock-extended into my testing routine.
The code snippet I aim to test is as follows:
class A {
constructor(private b: B){}
public test(x: string): string {
const res1 = this.b.compose({
name: x + '_foo'
})
const res2 = this.b.compose({
name: x + '_bar'
})
}
return res1 + '_' + res2
}
Here's my testing scenario:
test(() => {
const bMock: MockProxy<B> = mock<B>()
const a: A = new A(bMock)
bMock.compose.calledWith({
x: 'yoyo_foo'
}).mockReturnValueOnce(x + '_once')
bMock.compose.calledWith({
x: 'yoyo_bar'
}).mockReturnValueOnce(x + '_twice')
//ACT
const result = a.test('yoyo')
//ASSERT
expect(result).toBe('yoyo_foo_once_yoyo_bar_twice)
})
However, due to the use of calledWith
function with referential equality, the behavior is not as expected and the mocked object's compose
function returns undefined
.
Is there a way to rectify this issue? Perhaps by implementing shallow equality in some manner?
I am looking for a solution that allows me to utilize the calledWith
function with an object. Is such implementation possible?
Alternatively, is there another approach to mocking the compose function based on its input parameters?