@@ -6,6 +6,7 @@ import { Model } from "../../src";
66import { CancelledReason , DispatchResult , Zone } from "../../src/types" ;
77
88type DOMTarget = string | Element | Document | Window | null ;
9+ type ExpectResult = { pass : boolean ; message : ( ) => string } ;
910
1011declare global {
1112 namespace jest {
@@ -41,6 +42,11 @@ declare global {
4142 toHaveAttribute ( attribute : string , value : string ) : R ;
4243 toHaveStyle ( style : Record < string , string > ) : R ;
4344 }
45+ interface Expect {
46+ toBeBetween ( lower : number , upper : number ) : ExpectResult ;
47+ toBeSameColorAs ( expected : string , tolerance ?: number ) : ExpectResult ;
48+ toBeCloseTo ( expected : number , closeDigit ?: number ) : ExpectResult ;
49+ }
4450 }
4551}
4652
@@ -185,6 +191,20 @@ CancelledReasons: ${this.utils.printReceived(dispatchResult.reasons)}
185191 }
186192 return { pass : true , message : ( ) => "" } ;
187193 } ,
194+ toBeCloseTo ( received : number , expected : number , numDigits ?: number ) {
195+ numDigits = numDigits ?? - 2 ;
196+ const pass = Math . abs ( expected - received ) < 10 ** numDigits / 2 ;
197+ if ( pass ) {
198+ return { pass : true , message : ( ) => "" } ;
199+ }
200+ return {
201+ pass : false ,
202+ message : ( ) =>
203+ `Expected ${ received } to be close to ${ expected } with a tolerance of ${
204+ 10 ** numDigits / 2
205+ } `,
206+ } ;
207+ } ,
188208 toBeSameColorAs ( received : string , expected : string , tolerance : number = 0 ) {
189209 const pass = isSameColor ( received , expected , tolerance ) ;
190210 const message = ( ) =>
0 commit comments