-
Notifications
You must be signed in to change notification settings - Fork 533
/
usage-tracking.spec.ts
98 lines (87 loc) · 2.32 KB
/
usage-tracking.spec.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
trackUsage,
TestLimitReachedError,
} from '../../../../src/cli/commands/test/iac/local-execution/usage-tracking';
import { mocked } from 'ts-jest/utils';
import { NeedleResponse } from 'needle';
import { makeRequest } from '../../../../src/lib/request/request';
import { CustomError } from '../../../../src/lib/errors';
jest.mock('../../../../src/lib/request/request');
const mockedMakeRequest = mocked(makeRequest);
const results = [
{
meta: {
isPrivate: true,
},
result: {
cloudConfigResults: ['an issue'],
},
},
{
meta: {
isPrivate: false,
},
result: {
cloudConfigResults: [],
},
},
];
const org = 'test-org';
describe('tracking IaC test usage', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('does not throw an error when backend returns HTTP 200', async () => {
mockedMakeRequest.mockImplementationOnce(() => {
return Promise.resolve({
res: { statusCode: 200 } as NeedleResponse,
body: {
foo: 'bar',
},
});
});
await trackUsage(results, org);
expect(mockedMakeRequest.mock.calls.length).toEqual(1);
expect(mockedMakeRequest.mock.calls[0][0].qs).toEqual({ org });
expect(mockedMakeRequest.mock.calls[0][0].body).toEqual({
results: [
{
isPrivate: true,
issuesPrevented: 1,
},
{
isPrivate: false,
issuesPrevented: 0,
},
],
});
});
it('throws TestLimitReachedError when backend returns HTTP 429', async () => {
mockedMakeRequest.mockImplementationOnce(() => {
return Promise.resolve({
res: { statusCode: 429 } as NeedleResponse,
body: {
foo: 'bar',
},
});
});
await expect(trackUsage(results, org)).rejects.toThrow(
new TestLimitReachedError(),
);
});
it('throws CustomError when backend returns HTTP 500', async () => {
mockedMakeRequest.mockImplementationOnce(() => {
return Promise.resolve({
res: { statusCode: 500, body: { foo: 'bar' } } as NeedleResponse,
body: {
foo: 'bar',
},
});
});
await expect(trackUsage(results, org)).rejects.toThrow(
new CustomError(
'An error occurred while attempting to track test usage: {"foo":"bar"}',
),
);
});
});