-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
47 lines (45 loc) · 1.51 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const EventEmitter = require('eventemitter2');
const Plugin = require('./index.js');
describe('Plugin', () => {
describe('eventHandler', () => {
it('should log events to CloudWatch', async () => {
const ti2Events = new EventEmitter({ captureRejections: true, wildcard: true });
const cwEventsMock = {
putEvents: jest.fn().mockReturnValue({ promise: () => Promise.resolve() }),
};
const mockAWS = {
config: {
update: jest.fn(),
},
CloudWatchEvents: jest.fn().mockReturnValue(cwEventsMock),
};
jest.mock('aws-sdk', () => mockAWS);
const eventEmitterMock = {
on: (eventName, callback) => callback({ foo: 'bar' }),
};
const plugin = await (new Plugin({
events2log: 'request.*',
'AWS-ACCESS-KEY': 'ACCESS-KEY',
'AWS-SECRET-KEY': 'SECRET-KEY',
'AWS-REGION': 'us-west-2',
'Source': 'test'
}));
plugin.eventHandler(ti2Events);
ti2Events.emit('request.something', { foo: 'bar' });
await new Promise(resolve => setTimeout(resolve, 100)); // wait for async operations to complete
expect(cwEventsMock.putEvents).toHaveBeenCalled();
expect(cwEventsMock.putEvents.mock.calls[0][0]).toEqual({
Entries: [
{
Detail: JSON.stringify({
env: process.env.NODE_ENV,
foo: 'bar',
}),
DetailType: 'request.something',
Source: 'test',
},
],
});
});
});
});