Skip to content

Commit 8e019be

Browse files
authored
Merge pull request #37 from erezrokah/expose_cloudwatch_starttime
Expose cloudwatch startTime, startTime should be in UTC
2 parents 6ab2594 + 1da34f3 commit 8e019be

File tree

12 files changed

+129
-21
lines changed

12 files changed

+129
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-testing-library",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Chai and Jest matchers for aws services",
55
"scripts": {
66
"lint": "tslint 'src/**/*.ts'",

src/chai/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Asserts log message of a lambda function
9191
await expect({
9292
region: 'us-east-1',
9393
function: 'functionName',
94+
startTime: 0 /* optional (millis since epoch in UTC, defaults to now-1 hour) */,
9495
timeout: 0 /* optional (defaults to 2500) */,
9596
pollEvery: 0 /* optional (defaults to 500) */,
9697
}).to.have.log(

src/chai/cloudwatch.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import chai = require('chai');
2+
import * as common from '../common';
23
import './';
34
import cloudwatch from './cloudwatch';
45

5-
jest.mock('../common');
66
jest.mock('../utils/cloudwatch');
77
jest.mock('./utils', () => {
88
return { wrapWithRetries: jest.fn(f => f) };
99
});
1010

11+
jest.spyOn(Date, 'parse').mockImplementation(() => 12 * 60 * 60 * 1000);
12+
jest.spyOn(common, 'verifyProps');
13+
jest.spyOn(common, 'epochDateMinusHours');
14+
1115
chai.use(cloudwatch);
1216

1317
describe('cloudwatch', () => {
1418
describe('log', () => {
1519
const region = 'region';
1620
const functionName = 'functionName';
21+
const startTime = 12 * 60 * 60 * 1000;
1722

18-
const props = { region, function: functionName };
23+
const props = { region, function: functionName, startTime };
1924
const pattern = 'pattern';
2025

2126
beforeEach(() => {
@@ -52,11 +57,37 @@ describe('cloudwatch', () => {
5257
expect(filterLogEvents).toHaveBeenCalledWith(
5358
region,
5459
functionName,
60+
startTime,
5561
pattern,
5662
);
5763
expect(wrapWithRetries).toHaveBeenCalledTimes(1);
5864
});
5965

66+
test('startTime should be defaulted when not passed in', async () => {
67+
const { epochDateMinusHours, verifyProps } = require('../common');
68+
const { filterLogEvents } = require('../utils/cloudwatch');
69+
70+
filterLogEvents.mockReturnValue(Promise.resolve({ events: ['event'] }));
71+
epochDateMinusHours.mockReturnValue(11 * 60 * 60 * 1000);
72+
73+
const propsNoTime = { region, function: functionName };
74+
await chai.expect(propsNoTime).to.have.log(pattern);
75+
76+
expect(filterLogEvents).toHaveBeenCalledTimes(1);
77+
expect(filterLogEvents).toHaveBeenCalledWith(
78+
propsNoTime.region,
79+
propsNoTime.function,
80+
11 * 60 * 60 * 1000,
81+
pattern,
82+
);
83+
expect(verifyProps).toHaveBeenCalledTimes(1);
84+
expect(verifyProps).toHaveBeenCalledWith({ ...propsNoTime, pattern }, [
85+
'region',
86+
'function',
87+
'pattern',
88+
]);
89+
});
90+
6091
test('should pass on have log', async () => {
6192
const { filterLogEvents } = require('../utils/cloudwatch');
6293

src/chai/cloudwatch.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { verifyProps } from '../common';
1+
import { epochDateMinusHours, verifyProps } from '../common';
22
import { expectedProps, ICloudwatchProps } from '../common/cloudwatch';
33
import { filterLogEvents } from '../utils/cloudwatch';
44
import { wrapWithRetries } from './utils';
@@ -8,8 +8,17 @@ const attemptCloudwatch = async function(this: any, pattern: string) {
88

99
verifyProps({ ...props, pattern }, expectedProps);
1010

11-
const { region, function: functionName } = props;
12-
const { events } = await filterLogEvents(region, functionName, pattern);
11+
const {
12+
region,
13+
function: functionName,
14+
startTime = epochDateMinusHours(1),
15+
} = props;
16+
const { events } = await filterLogEvents(
17+
region,
18+
functionName,
19+
startTime,
20+
pattern,
21+
);
1322
const found = events.length > 0;
1423

1524
return {

src/common/cloudwatch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ICommonProps } from './';
22

33
export interface ICloudwatchProps extends ICommonProps {
44
function: string;
5+
startTime?: number;
56
}
67

78
export const expectedProps = ['region', 'function', 'pattern'];

src/common/index.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sleep, verifyProps } from './';
1+
import { epochDateMinusHours, sleep, verifyProps } from './';
22

33
jest.useFakeTimers();
44

@@ -26,4 +26,14 @@ describe('common', () => {
2626
expect(() => verifyProps({ id: 'value' }, ['id'])).not.toThrow();
2727
});
2828
});
29+
30+
describe('epochDateMinusHours', () => {
31+
jest.spyOn(Date, 'now').mockImplementation(() => 12 * 60 * 60 * 1000);
32+
33+
test('test implementation', () => {
34+
const actual = epochDateMinusHours(1);
35+
36+
expect(actual).toEqual(11 * 60 * 60 * 1000);
37+
});
38+
});
2939
});

src/common/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ export const verifyProps = (props: any, expectedProps: string[]) => {
1616
}
1717
}
1818
};
19+
20+
const hoursToMilliseconds = (hours: number) => hours * 60 * 60 * 1000;
21+
22+
export const epochDateMinusHours = (hours: number) => {
23+
return Date.now() - hoursToMilliseconds(hours);
24+
};

src/jest/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ expect.assertions(1); // makes sure the assertion was called
9898
await expect({
9999
region: 'us-east-1',
100100
function: 'functionName',
101+
startTime: 0 /* optional (millis since epoch in UTC, defaults to now-1 hour) */,
101102
timeout: 0 /* optional (defaults to 2500) */,
102103
pollEvery: 0 /* optional (defaults to 500) */,
103104
}).toHaveLog(

src/jest/cloudwatch.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import * as originalUtils from 'jest-matcher-utils';
22
import { EOL } from 'os';
3+
import * as common from '../common';
34
import { toHaveLog } from './cloudwatch';
45

5-
jest.mock('../common');
66
jest.mock('../utils/cloudwatch');
77
jest.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+
913
describe('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

src/jest/cloudwatch.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EOL } from 'os';
2-
import { verifyProps } from '../common';
32
import { expectedProps, ICloudwatchProps } from '../common/cloudwatch';
3+
import { epochDateMinusHours, verifyProps } from '../common/index';
44
import { filterLogEvents } from '../utils/cloudwatch';
55

66
export const toHaveLog = async function(
@@ -9,8 +9,11 @@ export const toHaveLog = async function(
99
pattern: string,
1010
) {
1111
verifyProps({ ...props, pattern }, expectedProps);
12-
13-
const { region, function: functionName } = props;
12+
const {
13+
region,
14+
function: functionName,
15+
startTime = epochDateMinusHours(1),
16+
} = props;
1417

1518
try {
1619
const printFunctionName = this.utils.printExpected(functionName);
@@ -20,7 +23,12 @@ export const toHaveLog = async function(
2023
const notHint = this.utils.matcherHint('.not.toHaveLog') + EOL + EOL;
2124
const hint = this.utils.matcherHint('.toHaveLog') + EOL + EOL;
2225

23-
const { events } = await filterLogEvents(region, functionName, pattern);
26+
const { events } = await filterLogEvents(
27+
region,
28+
functionName,
29+
startTime,
30+
pattern,
31+
);
2432
const found = events.length > 0;
2533
if (found) {
2634
// matching log found

0 commit comments

Comments
 (0)