@nevware21/tripwire - v0.1.8
    Preparing search index...

    Function changesByFunc

    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.

    The current IAssertScope object

    A function that returns a value to monitor, or an object containing the property to monitor

    The property name to monitor (used when first argument is not an object) or the expected change amount

    The expected change amount (sign is ignored, only absolute value matters)

    Optional message to display if the assertion fails

    The current IAssertScope.that object

    • 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.

      Type Parameters

      • R

      Parameters

      • this: IAssertScope

        The current IAssertScope object

      • func: () => any

        A function that returns a value to monitor

      • delta: number

        The expected change amount (sign is ignored, only absolute value matters)

      • OptionalevalMsg: MsgSource

        Optional message to display if the assertion fails

      Returns R

      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.

      Type Parameters

      • R
      • T = any

      Parameters

      • this: IAssertScope

        The current IAssertScope object

      • target: T

        An object containing the property to monitor

      • prop: keyof T

        The property name to monitor

      • delta: number | MsgSource

        The expected change amount (sign is ignored, only absolute value matters)

      • OptionalevalMsg: MsgSource

        Optional message to display if the assertion fails

      Returns R

      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