2026-06-16 17:10:58 -04:00
|
|
|
import {
|
|
|
|
|
jest,
|
|
|
|
|
describe,
|
|
|
|
|
it,
|
|
|
|
|
expect,
|
|
|
|
|
beforeAll,
|
|
|
|
|
beforeEach,
|
|
|
|
|
afterAll
|
|
|
|
|
} from '@jest/globals'
|
|
|
|
|
|
|
|
|
|
let info: string[] = []
|
|
|
|
|
|
|
|
|
|
// Mock @actions/core before loading retry-helper
|
|
|
|
|
jest.unstable_mockModule('@actions/core', () => ({
|
|
|
|
|
info: jest.fn((message: string) => {
|
|
|
|
|
info.push(message)
|
|
|
|
|
}),
|
|
|
|
|
debug: jest.fn(),
|
|
|
|
|
warning: jest.fn(),
|
|
|
|
|
error: jest.fn()
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
// Dynamic imports after mocking
|
|
|
|
|
const {RetryHelper} = await import('../src/retry-helper.js')
|
2020-02-13 13:25:46 -05:00
|
|
|
|
2019-12-12 13:16:16 -05:00
|
|
|
let retryHelper: any
|
|
|
|
|
|
|
|
|
|
describe('retry-helper tests', () => {
|
|
|
|
|
beforeAll(() => {
|
2020-02-13 13:25:46 -05:00
|
|
|
retryHelper = new RetryHelper(3, 0, 0)
|
2019-12-12 13:16:16 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
// Reset info
|
|
|
|
|
info = []
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
2020-02-13 13:25:46 -05:00
|
|
|
jest.restoreAllMocks()
|
2019-12-12 13:16:16 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('first attempt succeeds', async () => {
|
|
|
|
|
const actual = await retryHelper.execute(async () => {
|
|
|
|
|
return 'some result'
|
|
|
|
|
})
|
|
|
|
|
expect(actual).toBe('some result')
|
|
|
|
|
expect(info).toHaveLength(0)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('second attempt succeeds', async () => {
|
|
|
|
|
let attempts = 0
|
|
|
|
|
const actual = await retryHelper.execute(() => {
|
|
|
|
|
if (++attempts == 1) {
|
|
|
|
|
throw new Error('some error')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.resolve('some result')
|
|
|
|
|
})
|
|
|
|
|
expect(attempts).toBe(2)
|
|
|
|
|
expect(actual).toBe('some result')
|
|
|
|
|
expect(info).toHaveLength(2)
|
|
|
|
|
expect(info[0]).toBe('some error')
|
|
|
|
|
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('third attempt succeeds', async () => {
|
|
|
|
|
let attempts = 0
|
|
|
|
|
const actual = await retryHelper.execute(() => {
|
|
|
|
|
if (++attempts < 3) {
|
|
|
|
|
throw new Error(`some error ${attempts}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.resolve('some result')
|
|
|
|
|
})
|
|
|
|
|
expect(attempts).toBe(3)
|
|
|
|
|
expect(actual).toBe('some result')
|
|
|
|
|
expect(info).toHaveLength(4)
|
|
|
|
|
expect(info[0]).toBe('some error 1')
|
|
|
|
|
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
|
|
|
|
|
expect(info[2]).toBe('some error 2')
|
|
|
|
|
expect(info[3]).toMatch(/Waiting .+ seconds before trying again/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('all attempts fail succeeds', async () => {
|
|
|
|
|
let attempts = 0
|
2024-04-24 12:04:10 -04:00
|
|
|
let error: Error = null as unknown as Error
|
2019-12-12 13:16:16 -05:00
|
|
|
try {
|
|
|
|
|
await retryHelper.execute(() => {
|
|
|
|
|
throw new Error(`some error ${++attempts}`)
|
|
|
|
|
})
|
|
|
|
|
} catch (err) {
|
2021-10-19 09:52:57 -05:00
|
|
|
error = err as Error
|
2019-12-12 13:16:16 -05:00
|
|
|
}
|
|
|
|
|
expect(error.message).toBe('some error 3')
|
|
|
|
|
expect(attempts).toBe(3)
|
|
|
|
|
expect(info).toHaveLength(4)
|
|
|
|
|
expect(info[0]).toBe('some error 1')
|
|
|
|
|
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
|
|
|
|
|
expect(info[2]).toBe('some error 2')
|
|
|
|
|
expect(info[3]).toMatch(/Waiting .+ seconds before trying again/)
|
|
|
|
|
})
|
|
|
|
|
})
|