Skip to content

Instantly share code, notes, and snippets.

@marcelblijleven
Created October 4, 2017 08:26
Show Gist options
  • Save marcelblijleven/70058042eb2054f43c18a24b8516a79e to your computer and use it in GitHub Desktop.
Save marcelblijleven/70058042eb2054f43c18a24b8516a79e to your computer and use it in GitHub Desktop.
Jest: expect to be between two numbers
expect.extend({
toBeBetween(received, argumentOne, argumentTwo) {
if (argumentOne > argumentTwo) {
// Switch values
[argumentOne, argumentTwo] = [argumentTwo, argumentOne];
}
const pass = (received >= argumentOne && received <= argumentTwo);
if (pass) {
return {
message: () => (`expected ${received} not to be between ${argumentOne} and ${argumentTwo}`),
pass: true
};
} else {
return {
message: () => (`expected ${received} to be between ${argumentOne} and ${argumentTwo}`),
pass: false
}
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment