โž•toBe(Greater|Less)Than

Compare values by their worth

This set of comparisons validate that a value is greater than, less than, or equal to another value. The following assertions are true.

// 100 > 42
expect(100).toBeGreaterThan(42);

// 0 < 100
expect(0).toBeLessThan(100);

// 0 !> 100
expect(0).not.toBeGreaterThan(100);

// 1.0 >= 1.0
expect(1.0).toBeGreaterThanOrEqual(1.0);

// 1.0 !<= 0.0
expect(1.0).not.toBeLessThanOrEqual(0.0);

These assertions also work with reference types when the @operator(">" | "<" | ">=" | "<=") is used on a method in the class.

class Vec3 {
  constructor(public x: f64 = 0.0, public y: f64 = 0.0, public z: f64 = 0.0) {}

  @operator(">")
  protected __gt(other: Vec3): bool {
    return (
      this.x * this.x + this.y * this.y + this.z * this.z >
      other.x * other.x + other.y * other.y + other.z * other.z
    );
  }
}

// valid assertion because `@operator` was overloaded
expect(new Vec3(1, 2, 3)).toBeGreaterThan(new Vec3(0, 0, 0));

These methods are safe to use portably with jest, provided they aren't used with reference types.

Last updated