Is there any way to test the requests made by the graph client from the `@microsoft/microsoft-graph-client` library?

Mohammed Farhan 1 Reputation point
2021-01-27T11:36:42.383+00:00

Hello,

This is the code I used to initialize the graph client:

function getAuthenticatedClient(accessToken) {
  const client = graph.Client.init({
    // Use the provided access token to authenticate requests
    authProvider: done => {
      done(null, accessToken);
    },
  });

With this client I use the .api() function to make requests, so naturally I want to write tests for this. Lets say this is the code I wanna test for:

async function getLoggedInUserDetails(accessToken) {
   let client = getAuthenticatedClient(accessToken);

  const details = await client.api('/me').get();

  return details
}

Now for testing I've used the expect assertion from the chai.js as an assertion library and mocha to run it.
Lets say I use nock to mock the endpoint https://graph.microsoft.com/v1.0/me as:

let mockDetails = { 

displayName: 'FILLERSTRING',
displayEmail: 'FILLERSTRING'

}

let detailsRequest = nock('https://graph.microsoft.com/v1.0')
  .get('/me')
  .reply(200,mockDetails);

let d= await getLoggedInUser('TOKEN');

expect(d).to.equal(mockDetails);

detailsRequest.done()

This however does not work as the client doesn't accept the nock's response

So i was wondering if there was a way I could test the requests made by client.api()

Cheers

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,889 questions
{count} votes