Skip to main content

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();
});

Open browser consoleTests

.toBeNaN()​

Use .toBeNaN when checking a value is NaN.

This matcher does not coerce values. Strings, objects, undefined, and BigInt values are not NaN.

test('passes when value is NaN', () => {
expect(NaN).toBeNaN();
expect(1).not.toBeNaN();
expect('not a number').not.toBeNaN();
expect(1n).not.toBeNaN();
});

Open browser consoleTests

.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();
});

Open browser consoleTests

.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();
});

Open browser consoleTests

.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();
});

Open browser consoleTests

.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();
});

Open browser consoleTests

.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();
});

Open browser consoleTests

.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);
});

Open browser consoleTests

.toBeInteger()​

Use .toBeInteger when checking if a value is an integer Number or any BigInt.

This matcher does not coerce values. Numeric strings and boxed numbers are not integers.

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();
expect('1').not.toBeInteger();
});

Open browser consoleTests

.toBeInRange()​

Use .toBeInRange when checking if an array's elements all fall within a range (min inclusive, max exclusive). Now supports BigInt values.

Elements must be Number or BigInt primitives; values are not coerced. NaN, undefined, and sparse array holes fail the check. Mixed Number and BigInt values are supported, and empty arrays pass.

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);
expect(['4']).not.toBeInRange(4, 10);
expect([NaN]).not.toBeInRange(4, 10);
});

Open browser consoleTests

.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);
});

Open browser consoleTests

.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);
});

Open browser consoleTests