📏
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.
1
class LengthExample {
2
constructor(public length: i32 = 0) {}
3
}
4
5
const array = new Array<Vec3>(100);
6
const typedarray = new Uint8Array(42);
7
const buffer = new ArrayBuffer(29);
8
const custom = new LengthExample(50);
9
10
// Array<T> has a length
11
expect(array).toHaveLength(100);
12
13
// TypedArrays have a length
14
expect(typedarray).toHaveLength(42);
15
16
// ArrayBuffer uses byteLength
17
expect(buffer).toHaveLength(29);
18
19
// length is defined on LengthExample
20
expect(custom).toHaveLength(50);
Copied!
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 modified 1yr ago
Copy link