@@ -268,45 +268,77 @@ describe('fetch access token', () => {
268268 } ) ;
269269 } ) ;
270270
271- it ( 'throws an error when no access token was retrieved' , ( ) => {
271+ describe ( 'throws an error when no access token was retrieved' , ( ) => {
272+ const mockRes = { test : 1 } ;
272273 const username = 'user1' ;
273274 const password = 'nop' ;
274275
275- const originalConsoleLog = console . log ;
276- console . log = jest . fn ( ) ;
276+ beforeAll ( ( ) => {
277+ fetch . mockImplementation ( async ( ) => {
278+ return {
279+ json : ( ) => mockRes ,
280+ } ;
281+ } ) ;
282+ } ) ;
277283
278- const loginHandler = new handlers . Login ( {
279- auth0 : {
280- baseUrl : 'https://example.com' ,
281- apiAudience : 'https://example.com/' ,
282- clientId : 'abc' ,
283- clientSecret : 'xyz' ,
284- realm : 'test' ,
285- } ,
284+ afterAll ( ( ) => {
285+ fetch . mockRestore ( ) ;
286286 } ) ;
287- // Also test verbose logging.
288- loginHandler . verbose = true ;
289287
290- const mockRes = { test : 1 } ;
291- fetch . mockImplementation ( async ( ) => {
292- return {
293- json : ( ) => mockRes ,
294- } ;
288+ test ( 'when logging is verbose' , ( ) => {
289+ const originalConsoleLog = console . log ;
290+ console . log = jest . fn ( ) ;
291+
292+ const loginHandler = new handlers . Login ( {
293+ auth0 : {
294+ baseUrl : 'https://example.com' ,
295+ apiAudience : 'https://example.com/' ,
296+ clientId : 'abc' ,
297+ clientSecret : 'xyz' ,
298+ realm : 'test' ,
299+ } ,
300+ } ) ;
301+ // Also test verbose logging.
302+ loginHandler . verbose = true ;
303+
304+ return loginHandler
305+ . fetchAccessToken ( {
306+ username,
307+ password,
308+ } )
309+ . then ( data => {
310+ expect ( data ) . toBeUndefined ( ) ;
311+ } )
312+ . catch ( err => {
313+ expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
314+ expect ( console . log ) . toHaveBeenCalledWith ( mockRes ) ;
315+ console . log = originalConsoleLog ;
316+ expect ( err . message ) . toEqual ( 'Access token or expiration is missing' ) ;
317+ } ) ;
295318 } ) ;
296319
297- return loginHandler
298- . fetchAccessToken ( {
299- username,
300- password,
301- } )
302- . then ( data => {
303- expect ( data ) . toBeUndefined ( ) ;
304- } )
305- . catch ( err => {
306- expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
307- expect ( console . log ) . toHaveBeenCalledWith ( mockRes ) ;
308- console . log = originalConsoleLog ;
309- expect ( err . message ) . toEqual ( 'Access token or expiration is missing' ) ;
320+ test ( 'when logging is normal' , ( ) => {
321+ const loginHandler = new handlers . Login ( {
322+ auth0 : {
323+ baseUrl : 'https://example.com' ,
324+ apiAudience : 'https://example.com/' ,
325+ clientId : 'abc' ,
326+ clientSecret : 'xyz' ,
327+ realm : 'test' ,
328+ } ,
310329 } ) ;
330+
331+ return loginHandler
332+ . fetchAccessToken ( {
333+ username,
334+ password,
335+ } )
336+ . then ( data => {
337+ expect ( data ) . toBeUndefined ( ) ;
338+ } )
339+ . catch ( err => {
340+ expect ( err . message ) . toEqual ( 'Access token or expiration is missing' ) ;
341+ } ) ;
342+ } ) ;
311343 } ) ;
312344} ) ;
0 commit comments