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 [`method: PlaywrightAssertions.expectGeneric`]:
```js
import { test, expect } from '@playwright/test';
test('assert a value', async ({ page }) => {
const value = 1;
await expect(value).toBe(2);
});
```
## property: GenericAssertions.not
* since: v1.9
- returns: <[GenericAssertions]>
Makes the assertion check for the opposite condition. For example, the following code passes:
```js
const value = 1;
await expect(value).not.toBe(2);
```
## method: GenericAssertions.toBe
* since: v1.9
Compares value with [`param: expected`] by calling `Object.is`. This method compares objects by reference instead of their contents, similarly to the strict equality operator `===`.
**Usage**
```js
const value = { prop: 1 };
expect(value).toBe(value);
expect(value).not.toBe({});
expect(value.prop).toBe(1);
```
### param: GenericAssertions.toBe.expected
* since: v1.9
-`expected`<[any]>
Expected value.
## method: GenericAssertions.toBeCloseTo
* since: v1.9
Compares floating point numbers for approximate equality. Use this method instead of [`method: GenericAssertions.toBe`] when comparing floating point numbers.
The number of decimal digits after the decimal point that must be equal.
## method: GenericAssertions.toBeDefined
* since: v1.9
Ensures that value is not `undefined`.
**Usage**
```js
const value = null;
expect(value).toBeDefined();
```
## method: GenericAssertions.toBeFalsy
* since: v1.9
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**
```js
const value = null;
expect(value).toBeFalsy();
```
## method: GenericAssertions.toBeGreaterThan
* since: v1.9
Ensures that `value > expected` for number or big integer values.
Ensures that value is true in a boolean context, **anything but**`false`, `0`, `''`, `null`, `undefined` or `NaN`. Use this method when you don't care about the specific value.
**Usage**
```js
const value = { example: 'value' };
expect(value).toBeTruthy();
```
## method: GenericAssertions.toBeUndefined
* since: v1.9
Ensures that value is `undefined`.
**Usage**
```js
const value = undefined;
expect(value).toBeUndefined();
```
## method: GenericAssertions.toContain#1
* since: v1.9
Ensures that string value contains an expected substring. Comparison is case-sensitive.
**Usage**
```js
const value = 'Hello, World';
expect(value).toContain('World');
expect(value).toContain(',');
```
### param: GenericAssertions.toContain#1.expected
* since: v1.9
-`expected`<[string]>
Expected substring.
## method: GenericAssertions.toContain#2
* since: v1.9
Ensures that value is an `Array` or `Set` and contains an expected item.
**Usage**
```js
const value = [1, 2, 3];
expect(value).toContain(2);
expect(new Set(value)).toContain(2);
```
### param: GenericAssertions.toContain#2.expected
* since: v1.9
-`expected`<[any]>
Expected value in the collection.
## method: GenericAssertions.toContainEqual
* since: v1.9
Ensures that value is an `Array` or `Set` and contains an item equal to the expected.
For objects, this method recursively checks equality of all fields, rather than comparing objects by reference as performed by [`method: GenericAssertions.toContain#2`].
For primitive values, this method is equivalent to [`method: GenericAssertions.toContain#2`].
Compares contents of the value with contents of [`param: expected`], performing "deep equality" check.
For objects, this method recursively checks equality of all fields, rather than comparing objects by reference as performed by [`method: GenericAssertions.toBe`].
For primitive values, this method is equivalent to [`method: GenericAssertions.toBe`].
[`method: GenericAssertions.toEqual`] performs deep equality check that compares contents of the received and expected values. To ensure two objects reference the same instance, use [`method: GenericAssertions.toBe`] instead.
[`method: GenericAssertions.toEqual`] ignores `undefined` properties and array items, and does not insist on object types being equal. For stricter matching, use [`method: GenericAssertions.toStrictEqual`].
**Pattern matching**
[`method: GenericAssertions.toEqual`] can be also used to perform pattern matching on objects, arrays and primitive types, with the help of the following matchers:
* [`method: GenericAssertions.any`]
* [`method: GenericAssertions.anything`]
* [`method: GenericAssertions.arrayContaining`]
* [`method: GenericAssertions.closeTo`]
* [`method: GenericAssertions.objectContaining`]
* [`method: GenericAssertions.stringContaining`]
* [`method: GenericAssertions.stringMatching`]
Here is an example that asserts some of the values inside a complex object:
```js
expect({
list: [1, 2, 3],
obj: { prop: 'Hello world!', another: 'some other value' },
Ensures that property at provided `keyPath` exists on the object and optionally checks that property is equal to the [`param: expected`]. Equality is checked recursively, similarly to [`method: GenericAssertions.toEqual`].
Compares contents of the value with contents of [`param: expected`], performing "deep equality" check. Allows extra properties to be present in the value, unlike [`method: GenericAssertions.toEqual`], so you can check just a subset of object properties.
When comparing arrays, the number of items must match, and each item is checked recursively.
`expect.any()` matches any object instance created from the [`param: constructor`] or a corresponding primitive type. Use it inside [`method: GenericAssertions.toEqual`] to perform pattern matching.
`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 [`method: GenericAssertions.toEqual`] to perform pattern matching.
Expected array that is a subset of the received value.
## method: GenericAssertions.closeTo
* since: v1.9
Compares floating point numbers for approximate equality. Use this method inside [`method: GenericAssertions.toEqual`] to perform pattern matching. When just comparing two numbers, prefer [`method: GenericAssertions.toBeCloseTo`].
The number of decimal digits after the decimal point that must be equal.
## method: GenericAssertions.objectContaining
* since: v1.9
`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 [`method: GenericAssertions.toEqual`] to perform pattern matching. Object properties can be matchers to further relax the expectation. See examples.
**Usage**
```js
// 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' },
Expected object pattern that contains a subset of the properties.
## method: GenericAssertions.stringContaining
* since: v1.9
`expect.stringContaining()` matches a string that contains the expected substring. Use this method inside [`method: GenericAssertions.toEqual`] to perform pattern matching.
`expect.stringMatching()` matches a received string that in turn matches the expected pattern. Use this method inside [`method: GenericAssertions.toEqual`] to perform pattern matching.