Last active
July 15, 2022 00:19
-
-
Save bramses/05bac9225d4215c2a31def80788efb5c to your computer and use it in GitHub Desktop.
Extend Jest in TS and fix: ES2015 module syntax, Augmentations for the global scope (https://www.bramadams.dev/projects/jest-ts-extentions)
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
export {} | |
// https://jestjs.io/docs/expect#expectextendmatchers | |
declare global { | |
// eslint-disable-next-line @typescript-eslint/no-namespace | |
namespace jest { | |
interface Matchers<R> { | |
toBeWithinRange(a: number, b: number): R; | |
} | |
} | |
} | |
expect.extend({ | |
toBeWithinRange(received, floor, ceiling) { | |
const pass = received >= floor && received <= ceiling; | |
if (pass) { | |
return { | |
message: () => | |
`expected ${received} not to be within range ${floor} - ${ceiling}`, | |
pass: true, | |
}; | |
} else { | |
return { | |
message: () => | |
`expected ${received} to be within range ${floor} - ${ceiling}`, | |
pass: false, | |
}; | |
} | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment