number
toBeNumber
Use .toBeNumber when checking if a value is a Number.
test('passes when value is a number', () => { expect(1).toBeNumber(); expect(NaN).toBeNumber(); expect(Infinity).toBeNumber(); expect(true).not.toBeNumber(); });
Tests
.toBeNaN()
Use .toBeNaN when checking a value is NaN.
test('passes when value is NaN', () => { expect(NaN).toBeNaN(); expect(1).not.toBeNaN(); });
Tests
.toBeFinite()
Use .toBeFinite when checking if a value is a Number, not NaN or Infinity, or a BigInt.
test('passes when value is a finite number or bigint', () => { expect(1).toBeFinite(); expect(BigInt(123456789)).toBeFinite(); expect(Infinity).not.toBeFinite(); expect(NaN).not.toBeFinite(); });
Tests
.toBePositive()
Use .toBePositive when checking if a value is a positive Number or BigInt.
test('passes when value is a positive number or bigint', () => { expect(1).toBePositive(); expect(BigInt(42)).toBePositive(); expect(Infinity).not.toBePositive(); expect(-1).not.toBePositive(); expect(-BigInt(42)).not.toBePositive(); expect(NaN).not.toBePositive(); });
Tests
.toBeNegative()
Use .toBeNegative when checking if a value is a negative Number or BigInt.
test('passes when value is a negative number or bigint', () => { expect(-1).toBeNegative(); expect(BigInt(-123)).toBeNegative(); expect(-Infinity).not.toBeNegative(); expect(1).not.toBeNegative(); expect(BigInt(123)).not.toBeNegative(); expect(NaN).not.toBeNegative(); });
Tests
.toBeEven()
Use .toBeEven when checking if a value is an even Number or BigInt.
test('passes when value is an even number or bigint', () => { expect(2).toBeEven(); expect(BigInt(2)).toBeEven(); expect(BigInt(-4)).toBeEven(); expect(1).not.toBeEven(); expect(BigInt(1)).not.toBeEven(); expect(NaN).not.toBeEven(); });
Tests
.toBeOdd()
Use .toBeOdd when checking if a value is an odd Number or BigInt.
test('passes when value is an odd number or bigint', () => { expect(1).toBeOdd(); expect(BigInt(1)).toBeOdd(); expect(BigInt(-3)).toBeOdd(); expect(2).not.toBeOdd(); expect(BigInt(2)).not.toBeOdd(); expect(NaN).not.toBeOdd(); });
Tests
.toBeWithin(start, end)
Use .toBeWithin when checking if a number is in between the given bounds of: start (inclusive) and end (exclusive).
test('passes when number is within given bounds', () => { expect(1).toBeWithin(1, 3); expect(2).toBeWithin(1, 3); expect(3).not.toBeWithin(1, 3); });
Tests
.toBeInteger()
Use .toBeInteger when checking if a value is an integer Number or any BigInt.
test('passes when value is an integer or bigint', () => { expect(1).toBeInteger(); expect(1.0).toBeInteger(); expect(BigInt(42)).toBeInteger(); expect(1.1).not.toBeInteger(); });
Tests
.toBeInRange()
Use .toBeInRange when checking if an array's elements all fall within a range (min inclusive, max exclusive). Now supports BigInt values.
test('passes when all array elements are in range', () => { expect([4, 5, 7, 9]).toBeInRange(4, 10); expect([4n, 5n, 7n, 9n]).toBeInRange(4n, 10n); expect([4, 5, 10]).not.toBeInRange(4, 10); });
Tests
.toChangeBy()
Use .toChangeBy when checking if a function call changes a value by a specified amount. Now supports BigInt values.
test('passes when value changes by the specified amount', () => { let value = 0; let bigValue = BigInt(0); expect(() => { value += 2 }).toChangeBy(() => value, 2); expect(() => { bigValue += 2n }).toChangeBy(() => bigValue, 2n); expect(() => { value += 1 }).not.toChangeBy(() => value, 2); });
Tests
.toChangeTo()
Use .toChangeTo when checking if a function call changes a value to a specified value.
test('passes when value changes to the specified value', () => { let value = 0; let bigValue = BigInt(0); expect(() => { value = 2 }).toChangeTo(() => value, 2); expect(() => { bigValue = 42n }).toChangeTo(() => bigValue, 42n); expect(() => { value = 3 }).not.toChangeTo(() => value, 2); });
Tests