Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...