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

    Interface ChangeByFn

    Function signature for changeBy, increaseBy, and decreaseBy assertions. Can monitor either a getter function or an object property and validate the delta.

    Important Behavior Notes:

    • changeBy/changesBy: Sign of delta is ignored - only absolute value matters
    • increaseBy/increasesBy: Delta must be positive and value must increase
    • decreaseBy/decreasesBy: Delta should be positive (representing magnitude) and value must decrease

    0.1.5

    • Asserts that the target function changes the return value of the getter function by the specified delta.

      Behavior varies by assertion type:

      • changeBy/changesBy: Sign is ignored - only absolute value is compared
      • increaseBy/increasesBy: Delta must be positive, value must increase
      • decreaseBy/decreasesBy: Delta should be positive (magnitude), value must decrease

      Parameters

      • func: () => number

        A function that returns a value to monitor

      • delta: number

        The expected change amount (interpretation depends on assertion type)

      • OptionalevalMsg: MsgSource

        Optional message to display if the assertion fails

      Returns IAssertInst

      The assertion instance for chaining

      let value = 1;
      const getValue = () => value;
      const addFive = () => { value += 5; };
      const subtractThree = () => { value -= 3; };

      // changeBy - sign agnostic
      expect(addFive).to.changeBy(getValue, 5); // passes (delta of 5)
      expect(addFive).to.changeBy(getValue, -5); // also passes (absolute value of 5)

      // increaseBy - requires positive change
      expect(addFive).to.increaseBy(getValue, 5); // passes (increased by 5)

      // decreaseBy - use positive delta for magnitude
      expect(subtractThree).to.decreaseBy(getValue, 3); // passes (decreased by 3)
    • Asserts that the target function changes the specified property of the object by the specified delta.

      Behavior varies by assertion type:

      • changeBy/changesBy: Sign is ignored - only absolute value is compared
      • increaseBy/increasesBy: Delta must be positive, value must increase
      • decreaseBy/decreasesBy: Delta should be positive (magnitude), value must decrease

      Type Parameters

      • T

      Parameters

      • target: T

        The object containing the property to monitor

      • propName: keyof T

        The property name to monitor

      • delta: number

        The expected change amount (interpretation depends on assertion type)

      • OptionalevalMsg: MsgSource

        Optional message to display if the assertion fails

      Returns IAssertInst

      The assertion instance for chaining

      const obj = { val: 10 };
      const addTwo = () => { obj.val += 2; };
      const subtractFive = () => { obj.val -= 5; };

      // changeBy - sign agnostic
      expect(addTwo).to.changeBy(obj, 'val', 2); // passes (delta of 2)
      expect(addTwo).to.changeBy(obj, 'val', -2); // also passes (absolute value of 2)

      // increaseBy - requires positive change
      expect(addTwo).to.increaseBy(obj, 'val', 2); // passes (increased by 2)

      // decreaseBy - use positive delta for magnitude
      expect(subtractFive).to.decreaseBy(obj, 'val', 5); // passes (decreased by 5)