-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add retry to sendMetric to avoid connection errors
- Loading branch information
Showing
3 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import axios from 'axios' | ||
|
||
import { sendMetric } from './metrics' | ||
|
||
jest.mock('axios') | ||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
describe('when calling sendMetrics', () => { | ||
it('should call axios.post', async () => { | ||
const metric = { | ||
account: 'account', | ||
description: 'description', | ||
kind: 'kind', | ||
name: 'b2b-suite-buyerorg-data' as const, | ||
} | ||
|
||
await sendMetric(metric) | ||
|
||
expect(axios.post).toBeCalledTimes(1) | ||
}) | ||
|
||
it('should retry on failure', async () => { | ||
const metric = { | ||
account: 'account', | ||
description: 'description', | ||
kind: 'kind', | ||
name: 'b2b-suite-buyerorg-data' as const, | ||
} | ||
|
||
jest | ||
.spyOn(axios, 'post') | ||
.mockImplementation() | ||
.mockRejectedValueOnce(new Error('error')) | ||
|
||
await sendMetric(metric) | ||
|
||
expect(axios.post).toBeCalledTimes(2) | ||
}) | ||
|
||
it('should fail after max retries', async () => { | ||
const metric = { | ||
account: 'account', | ||
description: 'description', | ||
kind: 'kind', | ||
name: 'b2b-suite-buyerorg-data' as const, | ||
} | ||
|
||
jest | ||
.spyOn(axios, 'post') | ||
.mockImplementation() | ||
.mockRejectedValue(new Error('Error')) | ||
|
||
await expect(async () => { | ||
await sendMetric(metric) | ||
}).rejects.toThrowError('Error - after 2 retries.') | ||
|
||
expect(axios.post).toBeCalledTimes(3) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters