Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

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

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 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.

question from:https://stackoverflow.com/questions/65918534/is-there-any-way-to-test-the-requests-made-by-the-graph-client-from-the-microso

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...