github.com/ethereum-optimism/optimism@v1.7.2/packages/core-utils/test/common/test-utils.spec.ts (about) 1 import { assert } from 'chai' 2 3 /* Imports: Internal */ 4 import { expect } from '../setup' 5 import { expectApprox, awaitCondition } from '../../src' 6 7 describe('awaitCondition', () => { 8 it('should try the condition fn until it returns true', async () => { 9 let i = 0 10 const condFn = async () => { 11 i++ 12 return Promise.resolve(i === 2) 13 } 14 15 await awaitCondition(condFn, 50, 3) 16 expect(i).to.equal(2) 17 }) 18 19 it('should only try the configured number of attempts', async () => { 20 let i = 0 21 const condFn = async () => { 22 i++ 23 return Promise.resolve(i === 2) 24 } 25 26 try { 27 await awaitCondition(condFn, 50, 1) 28 } catch (e) { 29 return 30 } 31 32 assert.fail('Condition never failed, but it should have.') 33 }) 34 }) 35 36 describe('expectApprox', () => { 37 it('should pass when the actual number is higher, but within the expected range of the target', async () => { 38 expectApprox(119, 100, { 39 percentUpperDeviation: 20, 40 percentLowerDeviation: 20, 41 absoluteUpperDeviation: 20, 42 absoluteLowerDeviation: 20, 43 }) 44 }) 45 46 it('should pass when the actual number is lower, but within the expected range of the target', async () => { 47 expectApprox(81, 100, { 48 percentUpperDeviation: 20, 49 percentLowerDeviation: 20, 50 absoluteUpperDeviation: 20, 51 absoluteLowerDeviation: 20, 52 }) 53 }) 54 55 it('should throw an error when no deviation values are given', async () => { 56 try { 57 expectApprox(101, 100, {}) 58 assert.fail('expectApprox did not throw an error') 59 } catch (error) { 60 expect(error.message).to.equal( 61 'Must define at least one parameter to limit the deviation of the actual value.' 62 ) 63 } 64 }) 65 66 describe('should throw an error if the actual value is higher than expected', () => { 67 describe('... when only one upper bound value is defined', () => { 68 it('... and percentUpperDeviation sets the upper bound', async () => { 69 try { 70 expectApprox(121, 100, { 71 percentUpperDeviation: 20, 72 }) 73 assert.fail('expectApprox did not throw an error') 74 } catch (error) { 75 expect(error.message).to.equal( 76 'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true' 77 ) 78 } 79 }) 80 81 it('... and absoluteUpperDeviation sets the upper bound', async () => { 82 try { 83 expectApprox(121, 100, { 84 absoluteUpperDeviation: 20, 85 }) 86 assert.fail('expectApprox did not throw an error') 87 } catch (error) { 88 expect(error.message).to.equal( 89 'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true' 90 ) 91 } 92 }) 93 }) 94 95 describe('... when both values are defined', () => { 96 it('... and percentUpperDeviation sets the upper bound', async () => { 97 try { 98 expectApprox(121, 100, { 99 percentUpperDeviation: 20, 100 absoluteUpperDeviation: 30, 101 }) 102 assert.fail('expectApprox did not throw an error') 103 } catch (error) { 104 expect(error.message).to.equal( 105 'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true' 106 ) 107 } 108 }) 109 110 it('... and absoluteUpperDeviation sets the upper bound', async () => { 111 try { 112 expectApprox(121, 100, { 113 percentUpperDeviation: 30, 114 absoluteUpperDeviation: 20, 115 }) 116 assert.fail('expectApprox did not throw an error') 117 } catch (error) { 118 expect(error.message).to.equal( 119 'Actual value (121) is greater than the calculated upper bound of (120): expected false to be true' 120 ) 121 } 122 }) 123 }) 124 }) 125 126 describe('should throw an error if the actual value is lower than expected', () => { 127 describe('... when only one lower bound value is defined', () => { 128 it('... and percentLowerDeviation sets the lower bound', async () => { 129 try { 130 expectApprox(79, 100, { 131 percentLowerDeviation: 20, 132 }) 133 assert.fail('expectApprox did not throw an error') 134 } catch (error) { 135 expect(error.message).to.equal( 136 'Actual value (79) is less than the calculated lower bound of (80): expected false to be true' 137 ) 138 } 139 }) 140 141 it('... and absoluteLowerDeviation sets the lower bound', async () => { 142 try { 143 expectApprox(79, 100, { 144 absoluteLowerDeviation: 20, 145 }) 146 assert.fail('expectApprox did not throw an error') 147 } catch (error) { 148 expect(error.message).to.equal( 149 'Actual value (79) is less than the calculated lower bound of (80): expected false to be true' 150 ) 151 } 152 }) 153 }) 154 155 describe('... when both values are defined', () => { 156 it('... and percentLowerDeviation sets the lower bound', async () => { 157 try { 158 expectApprox(79, 100, { 159 percentLowerDeviation: 20, 160 absoluteLowerDeviation: 30, 161 }) 162 assert.fail('expectApprox did not throw an error') 163 } catch (error) { 164 expect(error.message).to.equal( 165 'Actual value (79) is less than the calculated lower bound of (80): expected false to be true' 166 ) 167 } 168 }) 169 170 it('... and absoluteLowerDeviation sets the lower bound', async () => { 171 try { 172 expectApprox(79, 100, { 173 percentLowerDeviation: 30, 174 absoluteLowerDeviation: 20, 175 }) 176 assert.fail('expectApprox did not throw an error') 177 } catch (error) { 178 expect(error.message).to.equal( 179 'Actual value (79) is less than the calculated lower bound of (80): expected false to be true' 180 ) 181 } 182 }) 183 }) 184 }) 185 })