question

MohammedFarhan-9146 avatar image
0 Votes"
MohammedFarhan-9146 asked saldana-msft edited

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

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-sdkmicrosoft-graph-identity
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Adding right tags/teams to assist!!

0 Votes 0 ·

0 Answers