11import * as originalUtils from 'jest-matcher-utils' ;
22import { EOL } from 'os' ;
3+ import * as common from '../common' ;
34import { toHaveLog } from './cloudwatch' ;
45
5- jest . mock ( '../common' ) ;
66jest . mock ( '../utils/cloudwatch' ) ;
77jest . spyOn ( console , 'error' ) ;
88
9+ jest . spyOn ( Date , 'parse' ) . mockImplementation ( ( ) => 12 * 60 * 60 * 1000 ) ;
10+ jest . spyOn ( common , 'verifyProps' ) ;
11+ jest . spyOn ( common , 'epochDateMinusHours' ) ;
12+
913describe ( 'cloudwatch matchers' , ( ) => {
1014 describe ( 'toHaveLog' , ( ) => {
1115 const matcherUtils = {
@@ -23,7 +27,8 @@ describe('cloudwatch matchers', () => {
2327 } ;
2428 const region = 'region' ;
2529 const functionName = 'functionName' ;
26- const props = { region, function : functionName } ;
30+ const startTime = 12 * 60 * 60 * 1000 ;
31+ const props = { region, function : functionName , startTime } ;
2732 const pattern = 'pattern' ;
2833
2934 beforeEach ( ( ) => {
@@ -45,6 +50,7 @@ describe('cloudwatch matchers', () => {
4550 expect ( filterLogEvents ) . toHaveBeenCalledWith (
4651 props . region ,
4752 props . function ,
53+ startTime ,
4854 pattern ,
4955 ) ;
5056 expect ( console . error ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -59,6 +65,33 @@ describe('cloudwatch matchers', () => {
5965 ] ) ;
6066 } ) ;
6167
68+ test ( 'startTime should be defaulted when not passed in' , async ( ) => {
69+ const { epochDateMinusHours, verifyProps } = require ( '../common' ) ;
70+ const { filterLogEvents } = require ( '../utils/cloudwatch' ) ;
71+
72+ const events : string [ ] = [ ] ;
73+ filterLogEvents . mockReturnValue ( Promise . resolve ( { events } ) ) ;
74+
75+ epochDateMinusHours . mockReturnValue ( 11 * 60 * 60 * 1000 ) ;
76+
77+ const propsNoTime = { region, function : functionName } ;
78+ await toHaveLog . bind ( matcherUtils ) ( propsNoTime , pattern ) ;
79+
80+ expect ( filterLogEvents ) . toHaveBeenCalledTimes ( 1 ) ;
81+ expect ( filterLogEvents ) . toHaveBeenCalledWith (
82+ propsNoTime . region ,
83+ propsNoTime . function ,
84+ 11 * 60 * 60 * 1000 ,
85+ pattern ,
86+ ) ;
87+ expect ( verifyProps ) . toHaveBeenCalledTimes ( 1 ) ;
88+ expect ( verifyProps ) . toHaveBeenCalledWith ( { ...propsNoTime , pattern } , [
89+ 'region' ,
90+ 'function' ,
91+ 'pattern' ,
92+ ] ) ;
93+ } ) ;
94+
6295 test ( 'should not pass when no events found' , async ( ) => {
6396 const { filterLogEvents } = require ( '../utils/cloudwatch' ) ;
6497
0 commit comments