11const LoggerController = require ( '../lib/Controllers/LoggerController' ) . LoggerController ;
22const WinstonLoggerAdapter = require ( '../lib/Adapters/Logger/WinstonLoggerAdapter' ) . WinstonLoggerAdapter ;
33const fs = require ( 'fs' ) ;
4+ const Config = require ( '../lib/Config' ) ;
45
56const loremFile = __dirname + '/support/lorem.txt' ;
67
@@ -23,34 +24,33 @@ describe("Cloud Code Logger", () => {
2324 // Note that helpers takes care of logout.
2425 // see helpers.js:afterEach
2526
26- it ( "should expose log to functions" , done => {
27- const logController = new LoggerController ( new WinstonLoggerAdapter ( ) ) ;
27+ it ( "should expose log to functions" , ( ) => {
28+ const config = Config . get ( 'test' ) ;
29+ const spy = spyOn ( config . loggerController , 'log' ) . and . callThrough ( ) ;
2830
2931 Parse . Cloud . define ( "loggerTest" , ( req , res ) => {
3032 req . log . info ( 'logTest' , 'info log' , { info : 'some log' } ) ;
3133 req . log . error ( 'logTest' , 'error log' , { error : 'there was an error' } ) ;
3234 res . success ( { } ) ;
3335 } ) ;
3436
35- Parse . Cloud . run ( 'loggerTest' ) . then ( ( ) => {
36- return logController . getLogs ( { from : Date . now ( ) - 500 , size : 1000 } ) ;
37- } ) . then ( ( res ) => {
38- expect ( res . length ) . not . toBe ( 0 ) ;
39- const lastLogs = res . slice ( 0 , 3 ) ;
40- const cloudFunctionMessage = lastLogs [ 0 ] ;
41- const errorMessage = lastLogs [ 1 ] ;
42- const infoMessage = lastLogs [ 2 ] ;
43- expect ( cloudFunctionMessage . level ) . toBe ( 'info' ) ;
44- expect ( cloudFunctionMessage . params ) . toEqual ( { } ) ;
45- expect ( cloudFunctionMessage . message ) . toMatch ( / R a n c l o u d f u n c t i o n l o g g e r T e s t f o r u s e r [ ^ ] * w i t h : \n { 2 } I n p u t : { } \n { 2 } R e s u l t : { } / ) ;
46- expect ( cloudFunctionMessage . functionName ) . toEqual ( 'loggerTest' ) ;
47- expect ( errorMessage . level ) . toBe ( 'error' ) ;
48- expect ( errorMessage . error ) . toBe ( 'there was an error' ) ;
49- expect ( errorMessage . message ) . toBe ( 'logTest error log' ) ;
50- expect ( infoMessage . level ) . toBe ( 'info' ) ;
51- expect ( infoMessage . info ) . toBe ( 'some log' ) ;
52- expect ( infoMessage . message ) . toBe ( 'logTest info log' ) ;
53- done ( ) ;
37+ return Parse . Cloud . run ( 'loggerTest' ) . then ( ( ) => {
38+ expect ( spy ) . toHaveBeenCalledTimes ( 3 ) ;
39+ const cloudFunctionMessage = spy . calls . all ( ) [ 2 ] ;
40+ const errorMessage = spy . calls . all ( ) [ 1 ] ;
41+ const infoMessage = spy . calls . all ( ) [ 0 ] ;
42+ expect ( cloudFunctionMessage . args [ 0 ] ) . toBe ( 'info' ) ;
43+ expect ( cloudFunctionMessage . args [ 1 ] [ 1 ] . params ) . toEqual ( { } ) ;
44+ expect ( cloudFunctionMessage . args [ 1 ] [ 0 ] ) . toMatch ( / R a n c l o u d f u n c t i o n l o g g e r T e s t f o r u s e r [ ^ ] * w i t h : \n { 2 } I n p u t : { } \n { 2 } R e s u l t : { } / ) ;
45+ expect ( cloudFunctionMessage . args [ 1 ] [ 1 ] . functionName ) . toEqual ( 'loggerTest' ) ;
46+ expect ( errorMessage . args [ 0 ] ) . toBe ( 'error' ) ;
47+ expect ( errorMessage . args [ 1 ] [ 2 ] . error ) . toBe ( 'there was an error' ) ;
48+ expect ( errorMessage . args [ 1 ] [ 0 ] ) . toBe ( 'logTest' ) ;
49+ expect ( errorMessage . args [ 1 ] [ 1 ] ) . toBe ( 'error log' ) ;
50+ expect ( infoMessage . args [ 0 ] ) . toBe ( 'info' ) ;
51+ expect ( infoMessage . args [ 1 ] [ 2 ] . info ) . toBe ( 'some log' ) ;
52+ expect ( infoMessage . args [ 1 ] [ 0 ] ) . toBe ( 'logTest' ) ;
53+ expect ( infoMessage . args [ 1 ] [ 1 ] ) . toBe ( 'info log' ) ;
5454 } ) ;
5555 } ) ;
5656
@@ -194,7 +194,8 @@ describe("Cloud Code Logger", () => {
194194 Parse . Cloud . run ( 'aFunction' , { foo : 'bar' } )
195195 . then ( null , ( ) => logController . getLogs ( { from : Date . now ( ) - 500 , size : 1000 } ) )
196196 . then ( logs => {
197- const log = logs [ 2 ] ;
197+ expect ( logs [ 0 ] . message ) . toBe ( 'it failed!' ) ;
198+ const log = logs [ 1 ] ;
198199 expect ( log . level ) . toEqual ( 'error' ) ;
199200 expect ( log . message ) . toMatch (
200201 / F a i l e d r u n n i n g c l o u d f u n c t i o n a F u n c t i o n f o r u s e r [ ^ ] * w i t h : \n { 2 } I n p u t : { " f o o " : " b a r " } \n { 2 } E r r o r : { " c o d e " : 1 4 1 , " m e s s a g e " : " i t f a i l e d ! " } / ) ;
@@ -243,4 +244,18 @@ describe("Cloud Code Logger", () => {
243244 } )
244245 . then ( null , e => done . fail ( e ) ) ;
245246 } ) ;
247+
248+ it ( 'should only log once for object not found' , async ( ) => {
249+ const config = Config . get ( 'test' ) ;
250+ const spy = spyOn ( config . loggerController , 'error' ) . and . callThrough ( ) ;
251+ try {
252+ const object = new Parse . Object ( 'Object' ) ;
253+ object . id = 'invalid'
254+ await object . fetch ( ) ;
255+ } catch ( e ) { /**/ }
256+ expect ( spy ) . toHaveBeenCalled ( ) ;
257+ expect ( spy . calls . count ( ) ) . toBe ( 1 ) ;
258+ const { args } = spy . calls . mostRecent ( ) ;
259+ expect ( args [ 0 ] ) . toBe ( 'Object not found.' ) ;
260+ } ) ;
246261} ) ;
0 commit comments