๐Ÿ“toHaveLength

Check the length of a value

This comparison verifies the length of a given object. This includes Array, TypedArray, ArrayBuffer, and custom classes that have a length property.

class LengthExample {
  constructor(public length: i32 = 0) {}
}

const array = new Array<Vec3>(100);
const typedarray = new Uint8Array(42);
const buffer = new ArrayBuffer(29);
const custom = new LengthExample(50);

// Array<T> has a length
expect(array).toHaveLength(100);

// TypedArrays have a length
expect(typedarray).toHaveLength(42);

// ArrayBuffer uses byteLength
expect(buffer).toHaveLength(29);

// length is defined on LengthExample
expect(custom).toHaveLength(50);

If this method is used with anything other than a reference type, it will result in a compile time error.

Note: ArrayBufferclasses are tested using the byteLength property.

This method is safe to use with jest, with the exception of using ArrayBuffer type references.

Last updated