damlReact.useQuery is not working for test cases

Hi Team,

We have recently upgraded our Daml version from 1.6.0 to 1.16.0.
UseQuery is working fine functionality wise but we see some issue with our test cases.
I am just giving structure of one test case where we are trying to use damlReact.useQuery

For eg -

damlReact.useQuery = jest.fn((template: any, argument, dependency) => {

  return {

    loading: false,

    contracts: []

  };

});

We get below error -
TypeError: Cannot set property useQuery of [object Object] which has only a getter

Could you please help us to resolve this issue.

Thanks,
Priyanka

1 Like

Hi @priyanka, this looks like you’re probably running into reactjs - TypeError during Jest's spyOn: Cannot set property getRequest of #<Object> which has only a getter - Stack Overflow or something very similar to that.

I’m not super familiar with jest but it looks like the most robust solution is to mock @daml/react completely rather than trying to override individual functions.

We do something similar in our own tests for mocking @daml/ledger daml/index.test.ts at 1b23f91a92ee937b67673dd60d592f2bc98d9ac0 · digital-asset/daml · GitHub

1 Like

Thanks for your reply.
Yes, issue was to related to mocking. I mocked data differently and it worked for me.

1 Like