Skip to content

Commit

Permalink
fix(test): fix flaky e2e tests (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
xrsv authored Jan 31, 2025
1 parent 3296bc9 commit 21ebba4
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions test/mocha/e2e/quote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ export function getTestAmount(currency: Currency): string {
}
}

const checkAbsoluteDifference = (actual: string, expected: string, threshold: number = 0.001) => {
const actualNum = parseFloat(actual)
const expectedNum = parseFloat(expected)
const percentDiff = Math.abs((actualNum - expectedNum) / expectedNum)
expect(
percentDiff <= threshold,
`Expected ${actual} to be within ${threshold * 100}% of ${expected}, but difference was ${percentDiff * 100}%`
).to.be.true
}

describe('quote', function () {
// Help with test flakiness by retrying.
this.retries(3)
Expand Down Expand Up @@ -796,7 +806,7 @@ describe('quote', function () {
expect(tokenInBefore.subtract(tokenInAfter).greaterThan(parseAmount('10', Ether.onChain(1)))).to.be.true
checkQuoteToken(tokenOutBefore, tokenOutAfter, CurrencyAmount.fromRawAmount(UNI_MAINNET, data.quote))
} else {
expect(tokenOutAfter.subtract(tokenOutBefore).toExact()).to.equal('10000')
checkAbsoluteDifference(tokenOutAfter.subtract(tokenOutBefore).toExact(), '10000', 0.0001)
// Can't easily check slippage for ETH due to gas costs effecting ETH balance.
}

Expand Down Expand Up @@ -850,7 +860,7 @@ describe('quote', function () {
expect(tokenInBefore.subtract(tokenInAfter).greaterThan(parseAmount('10', Ether.onChain(1)))).to.be.true
checkQuoteToken(tokenOutBefore, tokenOutAfter, CurrencyAmount.fromRawAmount(UNI_MAINNET, data.quote))
} else {
expect(tokenOutAfter.subtract(tokenOutBefore).toExact()).to.equal('10000')
checkAbsoluteDifference(tokenOutAfter.subtract(tokenOutBefore).toExact(), '10000', 0.0001)
// Can't easily check slippage for ETH due to gas costs effecting ETH balance.
}

Expand Down Expand Up @@ -2085,7 +2095,7 @@ describe('quote', function () {
expect(tokenInBefore.subtract(tokenInAfter).greaterThan(parseAmount('10', Ether.onChain(1)))).to.be.true
checkQuoteToken(tokenOutBefore, tokenOutAfter, CurrencyAmount.fromRawAmount(UNI_MAINNET, data.quote))
} else {
expect(tokenOutAfter.subtract(tokenOutBefore).toExact()).to.equal('10000')
checkAbsoluteDifference(tokenOutAfter.subtract(tokenOutBefore).toExact(), '10000', 0.0001)
// Can't easily check slippage for ETH due to gas costs effecting ETH balance.
}

Expand Down Expand Up @@ -2139,7 +2149,7 @@ describe('quote', function () {
checkQuoteToken(tokenOutBefore, tokenOutAfter, CurrencyAmount.fromRawAmount(UNI_MAINNET, data.quote))
expect(data.simulationError).to.equal(false)
} else {
expect(tokenOutAfter.subtract(tokenOutBefore).toExact()).to.equal('10000')
checkAbsoluteDifference(tokenOutAfter.subtract(tokenOutBefore).toExact(), '10000', 0.0001)
// Can't easily check slippage for ETH due to gas costs effecting ETH balance.
}

Expand Down

0 comments on commit 21ebba4

Please sign in to comment.