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


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 as:

let mockDetails = { 

displayName: 'FILLERSTRING',
displayEmail: 'FILLERSTRING'


let detailsRequest = nock('')

let d= await getLoggedInUser('TOKEN');



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()


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