Skip to main content

GenericAssertions

The GenericAssertions class provides assertion methods that can be used to make assertions about any values in the tests. A new instance of GenericAssertions is created by calling expect():

import { test, expect } from '@playwright/test';

test('assert a value', async ({ page }) => {
const value = 1;
expect(value).toBe(2);
});

Methods​

any​

Added in: v1.9 genericAssertions.any

expect.any() matches any object instance created from the constructor or a corresponding primitive type. Use it inside expect(value).toEqual() to perform pattern matching.

Usage

// Match instance of a class.
class Example {}
expect(new Example()).toEqual(expect.any(Example));

// Match any number.
expect({ prop: 1 }).toEqual({ prop: expect.any(Number) });

// Match any string.
expect('abc').toEqual(expect.any(String));

Arguments

  • constructor Function#

    Constructor of the expected object like ExampleClass, or a primitive boxed type like Number.


anything​

Added in: v1.9 genericAssertions.anything

expect.anything() matches everything except null and undefined. Use it inside expect(value).toEqual() to perform pattern matching.

Usage

const value = { prop: 1 };
expect(value).toEqual({ prop: expect.anything() });
expect(value).not.toEqual({ otherProp: expect.anything() });

arrayContaining​

Added in: v1.9 genericAssertions.arrayContaining

expect.arrayContaining() matches an array that contains all of the elements in the expected array, in any order. Note that received array may be a superset of the expected array and contain some extra elements.

Use this method inside expect(value).toEqual() to perform pattern matching.

Usage

expect([1, 2, 3]).toEqual(expect.arrayContaining([3, 1]));
expect([1, 2, 3]).not.toEqual(expect.arrayContaining([1, 4]));

Arguments

  • expected Array<Object>#

    Expected array that is a subset of the received value.


arrayOf​

Added in: v1.57 genericAssertions.arrayOf

expect.arrayOf() matches array of objects created from the constructor or a corresponding primitive type. Use it inside expect(value).toEqual() to perform pattern matching.

Usage

// Match instance of a class.
class Example {}
expect([new Example(), new Example()]).toEqual(expect.arrayOf(Example));

// Match any string.
expect(['a', 'b', 'c']).toEqual(expect.arrayOf(String));

Arguments

  • constructor Function#

    Constructor of the expected object like ExampleClass, or a primitive boxed type like Number.


closeTo​

Added in: v1.9 genericAssertions.closeTo

Compares floating point numbers for approximate equality. Use this method inside expect(value).toEqual() to perform pattern matching. When just comparing two numbers, prefer expect(value).toBeCloseTo().

Usage

expect({ prop: 0.1 + 0.2 }).not.toEqual({ prop: 0.3 });
expect({ prop: 0.1 + 0.2 }).toEqual({ prop: expect.closeTo(0.3, 5) });

Arguments

  • expected number#

    Expected value.

  • numDigits number (optional)#

    The number of decimal digits after the decimal point that must be equal.


objectContaining​

Added in: v1.9 genericAssertions.objectContaining

expect.objectContaining() matches an object that contains and matches all of the properties in the expected object. Note that received object may be a superset of the expected object and contain some extra properties.

Use this method inside expect(value).toEqual() to perform pattern matching. Object properties can be matchers to further relax the expectation. See examples.

Usage

// Assert some of the properties.
expect({ foo: 1, bar: 2 }).toEqual(expect.objectContaining({ foo: 1 }));

// Matchers can be used on the properties as well.
expect({ foo: 1, bar: 2 }).toEqual(expect.objectContaining({ bar: expect.any(Number) }));

// Complex matching of sub-properties.
expect({
list: [1, 2, 3],
obj: { prop: 'Hello world!', another: 'some other value' },
extra: 'extra',
}).toEqual(expect.objectContaining({
list: expect.arrayContaining([2, 3]),
obj: expect.objectContaining({ prop: expect.stringContaining('Hello') }),
}));

Arguments

  • expected Object#

    Expected object pattern that contains a subset of the properties.


stringContaining​

Added in: v1.9 genericAssertions.stringContaining

expect.stringContaining() matches a string that contains the expected substring. Use this method inside expect(value).toEqual() to perform pattern matching.

Usage

expect('Hello world!').toEqual(expect.stringContaining('Hello'));

Arguments

  • expected string#

    Expected substring.


stringMatching​

Added in: v1.9 genericAssertions.stringMatching

expect.stringMatching() matches a received string that in turn matches the expected pattern. Use this method inside expect(value).toEqual() to perform pattern matching.

Usage

expect('123ms').toEqual(expect.stringMatching(/\d+m?s/));

// Inside another matcher.
expect({
status: 'passed',
time: '123ms',
}).toEqual({
status: expect.stringMatching(/passed|failed/),
time: expect.stringMatching(/\d+m?s/),
});

Arguments

  • expected string | RegExp#

    Pattern that expected string should match.


toBe​

Added in: v1.9 genericAssertions.toBe

Compares value with expected by calling Object.is. This method compares objects by reference instead of their contents, similarly to the strict equality operator ===.

Usage

const value = { prop: 1 };
expect(value).toBe(value);
expect(value).not.toBe({});
expect(value.prop).toBe(1);

Arguments


toBeCloseTo​

Added in: v1.9 genericAssertions.toBeCloseTo

Compares floating point numbers for approximate equality. Use this method instead of expect(value).toBe() when comparing floating point numbers.

Usage

expect(0.1 + 0.2).not.toBe(0.3);
expect(0.1 + 0.2).toBeCloseTo(0.3, 5);

Arguments

  • expected number#

    Expected value.

  • numDigits number (optional)#

    The number of decimal digits after the decimal point that must be equal.


toBeDefined​

Added in: v1.9 genericAssertions.toBeDefined

Ensures that value is not undefined.

Usage

const value = null;
expect(value).toBeDefined();

toBeFalsy​

Added in: v1.9 genericAssertions.toBeFalsy

Ensures that value is false in a boolean context, one of false, 0, '', null, undefined or NaN. Use this method when you don't care about the specific value.

Usage

const value = null;
expect(value).toBeFalsy();

toBeGreaterThan​

Added in: v1.9 genericAssertions.toBeGreaterThan

Ensures that value > expected for number or big integer values.

Usage

const value = 42;
expect(value).toBeGreaterThan(1);

Arguments

  • expected number | [bigint]#

    The value to compare to.


toBeGreaterThanOrEqual​

Added in: v1.9 genericAssertions.toBeGreaterThanOrEqual

Ensures that value >= expected for number or big integer values.

Usage

const value = 42;
expect(value).toBeGreaterThanOrEqual(