I'm using jest-mock-extended in my tests.
I would like to test the following code:
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
}
And my test:
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)
})
But since the calledWith
function is using referential equality it doesn't work, the compose
function of the mocked object returns undefined
.
Is there a way to make it work? Maybe to enforce a shallow equality?
I would like to be able to use the calledWith
function with an object, is there a way?
Is there another way to mock the compose function according to its input?