Skip to content

Instantly share code, notes, and snippets.

@bramses
Last active July 15, 2022 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bramses/05bac9225d4215c2a31def80788efb5c to your computer and use it in GitHub Desktop.
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)
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