Skip to content

Commit

Permalink
test(sdk-router): error console cleanup (#3379)
Browse files Browse the repository at this point in the history
* test: ignore console.error in negative API cases

* test: better fetch mock to simulate empty list of quotes

* test: partially apply  Rabbit suggestion

* test: retry for flaky tests
  • Loading branch information
ChiTimesChi authored Nov 7, 2024
1 parent c892e69 commit 27bd1d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/sdk-router/src/rfq/api.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { getAllQuotes } from './api'

global.fetch = require('node-fetch')

// Retry the flaky tests up to 3 times
jest.retryTimes(3)

describe('getAllQuotes', () => {
it('Integration test', async () => {
const result = await getAllQuotes()
Expand Down
13 changes: 13 additions & 0 deletions packages/sdk-router/src/rfq/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,28 @@ describe('getAllQuotes', () => {
})

describe('Returns an empty array', () => {
beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {
// Do nothing
})
})

afterEach(() => {
jest.restoreAllMocks()
})

it('when the response is not ok', async () => {
fetchMock.mockResponseOnce(JSON.stringify(quotesAPI), { status: 500 })
const result = await getAllQuotes()
expect(result).toEqual([])
expect(console.error).toHaveBeenCalled()
})

it('when fetch throws an error', async () => {
fetchMock.mockRejectOnce(new Error('Error fetching quotes'))
const result = await getAllQuotes()
expect(result).toEqual([])
expect(console.error).toHaveBeenCalled()
})

it('when the response takes too long to return', async () => {
Expand All @@ -94,6 +106,7 @@ describe('getAllQuotes', () => {
)
const result = await getAllQuotes()
expect(result).toEqual([])
expect(console.error).toHaveBeenCalled()
})
})
})
6 changes: 5 additions & 1 deletion packages/sdk-router/src/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ import * as operations from './operations'
// Override fetch to exclude RFQ from tests
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve({}),
ok: true,
json: () => Promise.resolve([]),
})
) as any

// Retry the flaky tests up to 3 times
jest.retryTimes(3)

const EXPECTED_GAS_DROP: { [chainId: number]: BigNumber } = {
[SupportedChainId.ETH]: BigNumber.from(0),
// TODO: reenable once both ARB airdrops are adjusted
Expand Down

0 comments on commit 27bd1d2

Please sign in to comment.