Skip to content

Commit 6ceb3ea

Browse files
authored
Merge pull request #32 from erezrokah/limit_toHaveLog_results_by_startTime
add startTime filter to filterLogEvents
2 parents ec86b8d + 5880752 commit 6ceb3ea

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
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.1",
3+
"version": "1.0.2",
44
"description": "Chai and Jest matchers for aws services",
55
"scripts": {
66
"lint": "tslint 'src/**/*.ts'",

src/utils/cloudwatch.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jest.mock('aws-sdk', () => {
1515
return { CloudWatchLogs };
1616
});
1717

18+
jest.spyOn(Date, 'now').mockImplementation(() => 12 * 60 * 60 * 1000);
19+
1820
describe('cloudwatch utils', () => {
1921
const AWS = require('aws-sdk');
2022
const cloudWatchLogs = AWS.CloudWatchLogs;
@@ -91,6 +93,7 @@ describe('cloudwatch utils', () => {
9193
interleaved: true,
9294
limit: 1,
9395
logGroupName,
96+
startTime: 11 * 60 * 60 * 1000,
9497
});
9598
expect(actual).toEqual({ events });
9699
});

src/utils/cloudwatch.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import AWS = require('aws-sdk');
22

33
const getLogGroupName = (functionName: string) => `/aws/lambda/${functionName}`;
44

5+
const hoursToMilliseconds = (hours: number) => hours * 60 * 60 * 1000;
6+
57
export const filterLogEvents = async (
68
region: string,
79
functionName: string,
@@ -10,13 +12,15 @@ export const filterLogEvents = async (
1012
const cloudWatchLogs = new AWS.CloudWatchLogs({ region });
1113
const logGroupName = getLogGroupName(functionName);
1214
const filterPattern = `"${pattern}"`; // enclose with "" to support special characters
15+
const startTime = Date.now() - hoursToMilliseconds(1);
1316

1417
const { events = [] } = await cloudWatchLogs
1518
.filterLogEvents({
1619
filterPattern,
1720
interleaved: true,
1821
limit: 1,
1922
logGroupName,
23+
startTime,
2024
})
2125
.promise();
2226

0 commit comments

Comments
 (0)