forked from kamranahmedse/aws-cost-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslack.test.ts
83 lines (66 loc) · 2.55 KB
/
slack.test.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import AWS from 'aws-sdk';
import { AWSConfig } from './config';
import { formatServiceBreakdown } from './slack';
import { generateMockedCostByService } from '../testUtils';
import { getTotalCosts } from '../cost';
import AWSMock from 'aws-sdk-mock';
const costDataLength = 65;
const fixedToday = '2024-05-11'; // cost of 'this month' will be sum of 10 days from May 1 to May 10 ('today' is omitted because its cost is incomplete)
const awsConfig: AWSConfig = {
credentials: {
accessKeyId: 'testAccessKeyId',
secretAccessKey: 'testSecretAccessKey',
sessionToken: 'testSessionToken',
},
region: 'us-east-1',
};
jest.mock('../logger', () => ({
...jest.requireActual('../logger'),
showSpinner: jest.fn(),
}));
const mockedCostByService = generateMockedCostByService(
fixedToday,
costDataLength,
);
beforeAll(() => {
AWSMock.setSDKInstance(AWS);
});
afterAll(() => {
AWSMock.restore();
});
beforeEach(() => {
jest.useFakeTimers('modern');
jest.setSystemTime(new Date(fixedToday).getTime());
});
afterEach(() => {
jest.useRealTimers();
});
AWSMock.mock('CostExplorer', 'getCostAndUsage', (params, callback) => {
callback(null, mockedCostByService);
});
describe('formatServiceBreakdown', () => {
it('should return service breakdown of "yesterday"', async () => {
const totalCosts = await getTotalCosts(awsConfig);
const result = formatServiceBreakdown(totalCosts, 'yesterday');
// cost value is defined in generateMockedCostByService
expect(result).toEqual('> service2: `$110.00`\n' + '> service1: `$1.10`');
});
it('should return service breakdown of "Last 7 days"', async () => {
const totalCosts = await getTotalCosts(awsConfig);
const result = formatServiceBreakdown(totalCosts, 'last7Days');
// cost value is defined in generateMockedCostByService
expect(result).toEqual('> service2: `$770.00`\n' + '> service1: `$7.70`');
});
it('should return service breakdown of "This Month"', async () => {
const totalCosts = await getTotalCosts(awsConfig);
const result = formatServiceBreakdown(totalCosts, 'thisMonth');
// cost value is defined in generateMockedCostByService
expect(result).toEqual('> service2: `$1100.00`\n' + '> service1: `$11.00`');
});
it('should return service breakdown of "Last Month"', async () => {
const totalCosts = await getTotalCosts(awsConfig);
const result = formatServiceBreakdown(totalCosts, 'lastMonth');
// cost value is defined in generateMockedCostByService
expect(result).toEqual('> service2: `$3000.00`\n' + '> service1: `$30.00`');
});
});