Asserts that executing the target function changes the monitored value by a specific delta. Important: The sign of the delta is ignored - only the absolute value is compared. This means changeBy(5) and changeBy(-5) are equivalent and will both match a change of ±5.
The current IAssertScope object
A function that returns a value to monitor
The expected change amount (sign is ignored, only absolute value matters)
OptionalevalMsg: MsgSource
Optional message to display if the assertion fails
The current IAssertScope.that object
let value = 10;
const getValue = () => value;
const addFive = () => { value += 5; };
const subtractFive = () => { value -= 5; };
expect(addFive).to.changeBy(getValue, 5); // Passes - delta is +5
expect(addFive).to.changeBy(getValue, -5); // Also passes - absolute value is 5
expect(subtractFive).to.changeBy(getValue, 5); // Passes - absolute value is 5
expect(subtractFive).to.changeBy(getValue, -5); // Also passes - absolute value is 5
Asserts that executing the target function changes the monitored value by a specific delta. Important: The sign of the delta is ignored - only the absolute value is compared. This means changeBy(5) and changeBy(-5) are equivalent and will both match a change of ±5.
The current IAssertScope object
An object containing the property to monitor
The property name to monitor
The expected change amount (sign is ignored, only absolute value matters)
OptionalevalMsg: MsgSource
Optional message to display if the assertion fails
The current IAssertScope.that object
const obj = { val: 10 };
const addFive = () => { obj.val += 5; };
const subtractFive = () => { obj.val -= 5; };
expect(addFive).to.changeBy(obj, 'val', 5); // Passes - delta is +5
expect(addFive).to.changeBy(obj, 'val', -5); // Also passes - absolute value is 5
expect(subtractFive).to.changeBy(obj, 'val', 5); // Passes - absolute value is 5
Asserts that executing the target function changes the monitored value by a specific delta. The sign of the delta is ignored - only the absolute value is compared.
Param: this
The current IAssertScope object
Param: targetOrFn
A function that returns a value to monitor, or an object containing the property to monitor
Param: propOrDelta
The property name to monitor (used when first argument is not an object) or the expected change amount
Param: delta
The expected change amount (sign is ignored, only absolute value matters)
Param: evalMsg
Optional message to display if the assertion fails
Returns
The current IAssertScope.that object