how to spy on methods of object returned by a mocked/stubbed function with sinon

Viewed 15

In my typescript/node.js source code, I have the following lines:

const client = await getClient();
await client.propertyOne.methodTwo({
   name: 'Jim',
   age: 23
})

where getClient is a function that returns a user defined object such that it has a property called propertyOne with a method called methodTwo.

When I test this file using sinon, I want to stub the getClient function, and ensure that the methodTwo method is called with the expected argument. However, I am not sure how to do this, as the client object will be stubbed. I have tried the below in the test file:

import * as GetClient from '../client';
const clientStub = sinon.stub(GetClient, 'getClient');
clientStub.resolves();

But once the clientStub resolves, I have no idea how to ensure that methodTwo is called with the right arguments - does anyone know how to test this?

0 Answers
Related