Skip to content

Commit 577b407

Browse files
committed
Update unit tests
1 parent 404c8c2 commit 577b407

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

test/analytics.test.ts

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import * as apiUtils from '../src/apiUtils'
3+
4+
// Mock dependencies
5+
vi.mock('../src/apiUtils', () => ({
6+
apiRequest: vi.fn().mockResolvedValue({}),
7+
DEFAULT_RETRY_OPTIONS: {
8+
maxRetries: 3,
9+
initialDelayMs: 1000,
10+
backoffFactor: 2,
11+
nonRetryStatusCodes: [400, 401, 403, 404, 405, 422],
12+
},
13+
}))
14+
15+
// Import the module under test
16+
import { UserSpecifiedAnalyticsData } from '../src/analytics'
17+
18+
describe('analytics', () => {
19+
// Save original environment and console.error
20+
const originalEnv = process.env
21+
const originalConsoleError = console.error
22+
23+
// Mock data
24+
const mockUserSpecifiedData: UserSpecifiedAnalyticsData = {
25+
totalTestScriptsExecuted: 5,
26+
isCloudRun: true,
27+
isUsingFlags: true,
28+
isUsingInspectFlags: false,
29+
failFast: true,
30+
commentOnPr: true,
31+
parallelFlag: false,
32+
cloudRunLocally: false,
33+
onlyVerifyScripts: false,
34+
}
35+
36+
beforeEach(() => {
37+
// Reset mocks and environment before each test
38+
vi.resetAllMocks()
39+
process.env = { ...originalEnv }
40+
console.error = vi.fn()
41+
42+
// Set up environment variables
43+
process.env.GITHUB_ACTION = 'test-action'
44+
process.env.GITHUB_WORKFLOW = 'test-workflow'
45+
})
46+
47+
afterEach(() => {
48+
// Restore original environment and console.error after each test
49+
process.env = originalEnv
50+
console.error = originalConsoleError
51+
vi.clearAllMocks()
52+
})
53+
54+
describe('sendAnalytics', () => {
55+
it('should send analytics data with correct properties', async () => {
56+
// We need to use a dynamic import to avoid hoisting issues
57+
const { sendAnalytics } = await import('../src/analytics')
58+
59+
await sendAnalytics(mockUserSpecifiedData)
60+
61+
// Check that the API request was made with the right data
62+
expect(apiUtils.apiRequest).toHaveBeenCalledTimes(1)
63+
expect(apiUtils.apiRequest).toHaveBeenCalledWith(
64+
expect.any(String),
65+
{
66+
method: 'POST',
67+
body: expect.stringContaining('"source":"github-action"'),
68+
},
69+
expect.objectContaining({
70+
maxRetries: 1,
71+
})
72+
)
73+
})
74+
75+
it('should use the default analytics URL if not provided in environment', async () => {
76+
delete process.env.GRAFANA_ANALYTICS_URL
77+
78+
// We need to use a dynamic import to avoid hoisting issues
79+
const { sendAnalytics } = await import('../src/analytics')
80+
81+
await sendAnalytics(mockUserSpecifiedData)
82+
83+
expect(apiUtils.apiRequest).toHaveBeenCalledWith(
84+
'https://stats.grafana.org',
85+
expect.any(Object),
86+
expect.any(Object)
87+
)
88+
})
89+
90+
it('should use the custom analytics URL if provided in environment', async () => {
91+
process.env.GRAFANA_ANALYTICS_URL = 'https://custom-stats.example.com'
92+
93+
// We need to use a dynamic import to avoid hoisting issues
94+
const { sendAnalytics } = await import('../src/analytics')
95+
96+
await sendAnalytics(mockUserSpecifiedData)
97+
98+
expect(apiUtils.apiRequest).toHaveBeenCalledWith(
99+
'https://custom-stats.example.com',
100+
expect.any(Object),
101+
expect.any(Object)
102+
)
103+
})
104+
105+
it('should handle errors when sending analytics', async () => {
106+
// Mock apiRequest to throw an error
107+
vi.mocked(apiUtils.apiRequest).mockRejectedValueOnce(
108+
new Error('Network error')
109+
)
110+
111+
// We need to use a dynamic import to avoid hoisting issues
112+
const { sendAnalytics } = await import('../src/analytics')
113+
114+
// Call the function
115+
await sendAnalytics(mockUserSpecifiedData)
116+
117+
// Verify that the error was handled and logged
118+
expect(console.error).toHaveBeenCalledWith(
119+
'Error sending analytics:',
120+
expect.any(Error)
121+
)
122+
})
123+
124+
it('should generate a unique usage stats ID when GitHub variables are missing', async () => {
125+
// Remove GitHub environment variables
126+
delete process.env.GITHUB_ACTION
127+
delete process.env.GITHUB_WORKFLOW
128+
129+
// We need to use a dynamic import to avoid hoisting issues
130+
const { sendAnalytics } = await import('../src/analytics')
131+
132+
await sendAnalytics(mockUserSpecifiedData)
133+
134+
// Just verify that the API request was made successfully
135+
expect(apiUtils.apiRequest).toHaveBeenCalledTimes(1)
136+
// The body should contain a usageStatsId even though GitHub variables are missing
137+
expect(apiUtils.apiRequest).toHaveBeenCalledWith(
138+
expect.any(String),
139+
{
140+
method: 'POST',
141+
body: expect.stringContaining('"usageStatsId"'),
142+
},
143+
expect.any(Object)
144+
)
145+
})
146+
})
147+
})

test/index.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ vi.mock('../src/utils', () => ({
3737
findTestsToRun: vi.fn(),
3838
}))
3939

40+
vi.mock('../src/analytics', () => ({
41+
sendAnalytics: vi.fn(),
42+
}))
43+
4044
vi.mock('../src/k6helper', () => ({
4145
validateTestPaths: vi.fn(),
4246
executeRunK6Command: vi.fn(),

0 commit comments

Comments
 (0)