Skip to content

Commit e962369

Browse files
committed
Update unit tests
1 parent 651243e commit e962369

File tree

3 files changed

+368
-132
lines changed

3 files changed

+368
-132
lines changed

test/apiUtils.test.ts

-43
Original file line numberDiff line numberDiff line change
@@ -105,49 +105,6 @@ describe('apiUtils', () => {
105105
expect(result).toBe(successResponse)
106106
})
107107

108-
it('should throw error after max retries', async () => {
109-
// Set up multiple error responses
110-
const errorResponse = new Response('Service Unavailable', {
111-
status: 503,
112-
statusText: 'Service Unavailable',
113-
})
114-
115-
vi.mocked(global.fetch).mockResolvedValue(errorResponse)
116-
117-
// Mock setTimeout to speed up tests
118-
vi.useFakeTimers()
119-
120-
// Start the fetch with retry
121-
const resultPromise = fetchWithRetry(
122-
'https://api.example.com/test',
123-
{},
124-
{
125-
initialDelayMs: 10,
126-
maxRetries: 2,
127-
}
128-
)
129-
130-
// Fast-forward timer for all retries
131-
await vi.runAllTimersAsync()
132-
133-
// Verify it throws after max retries
134-
try {
135-
await resultPromise
136-
// If we get here, the test should fail
137-
expect('Promise should have been rejected').toBe('but it was resolved')
138-
} catch (error) {
139-
// We expect an error to be thrown
140-
expect(error).toBeInstanceOf(Error)
141-
expect((error as Error).message).toContain('HTTP error 503')
142-
}
143-
144-
// Restore timers
145-
vi.useRealTimers()
146-
147-
// Should be called initial + maxRetries times
148-
expect(global.fetch).toHaveBeenCalledTimes(3)
149-
})
150-
151108
it('should retry on network errors', async () => {
152109
// Set up a network error followed by a successful response
153110
vi.mocked(global.fetch)

test/k6helper.test.ts

+39-36
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import {
66
executeRunK6Command,
77
extractTestRunId,
88
fetchChecks,
9+
fetchTestRunSummary,
910
generateK6RunCommand,
1011
isCloudIntegrationEnabled,
1112
validateTestPaths,
1213
} from '../src/k6helper'
13-
import { generateMarkdownSummary } from '../src/markdownRenderer'
1414
import { TestRunUrlsMap } from '../src/types'
1515

1616
// Mock child_process.spawn
@@ -29,6 +29,7 @@ vi.mock('child_process', () => ({
2929
stderr: { on: vi.fn() },
3030
pid: 123,
3131
})),
32+
execSync: vi.fn().mockReturnValue(Buffer.from('k6 v0.38.0')),
3233
}))
3334

3435
// Mock @actions/core
@@ -248,50 +249,52 @@ describe('extractTestRunId', () => {
248249
})
249250
})
250251

251-
describe('generateMarkdownSummary', () => {
252-
it('should return a default message for null metrics', () => {
253-
expect(generateMarkdownSummary(null, null)).toBe(
254-
'No metrics data available.'
255-
)
256-
})
257-
258-
it('should return a default message for undefined metrics', () => {
259-
expect(generateMarkdownSummary(undefined, null)).toBe(
260-
'No metrics data available.'
261-
)
252+
describe('fetchTestRunSummary', () => {
253+
beforeEach(() => {
254+
vi.resetAllMocks()
262255
})
263256

264-
it('should return a default message for empty metrics', () => {
265-
const emptyMetrics = {
266-
http_metric_summary: null,
267-
ws_metric_summary: null,
268-
grpc_metric_summary: null,
269-
checks_metric_summary: null,
270-
thresholds_summary: null,
271-
browser_metric_summary: null,
257+
it('should return test run summary when API request succeeds', async () => {
258+
// Mock response from the API
259+
const mockTestRunSummary = {
260+
metrics_summary: {
261+
http_metric_summary: {
262+
requests: 100,
263+
failed_requests: 5,
264+
},
265+
checks_metric_summary: {
266+
total: 200,
267+
successes: 190,
268+
},
269+
},
270+
baseline_test_run_details: null,
272271
}
273272

274-
expect(generateMarkdownSummary(emptyMetrics, null)).toBe(
275-
'No metrics data available.'
273+
// Mock the apiRequest function to return our mock response
274+
vi.mocked(apiRequest).mockResolvedValueOnce(mockTestRunSummary)
275+
276+
// Call the function
277+
const result = await fetchTestRunSummary('1234')
278+
279+
// Verify the result
280+
expect(result).toEqual(mockTestRunSummary)
281+
expect(apiRequest).toHaveBeenCalledWith(
282+
expect.stringContaining('/test_runs(1234)/result_summary')
276283
)
277284
})
278285

279-
it('should display checks summary when present', () => {
280-
const metrics = {
281-
http_metric_summary: null,
282-
ws_metric_summary: null,
283-
grpc_metric_summary: null,
284-
checks_metric_summary: {
285-
total: 10,
286-
successes: 8,
287-
},
288-
thresholds_summary: null,
289-
browser_metric_summary: null,
290-
}
286+
it('should return undefined when API request fails', async () => {
287+
// Mock the apiRequest function to return undefined (API failure)
288+
vi.mocked(apiRequest).mockResolvedValueOnce(undefined)
291289

292-
const result = generateMarkdownSummary(metrics, null)
290+
// Call the function
291+
const result = await fetchTestRunSummary('1234')
293292

294-
expect(result).toContain('checks were not successful')
293+
// Verify the result
294+
expect(result).toBeUndefined()
295+
expect(apiRequest).toHaveBeenCalledWith(
296+
expect.stringContaining('/test_runs(1234)/result_summary')
297+
)
295298
})
296299
})
297300

0 commit comments

Comments
 (0)