diff --git a/docs/src/api/class-genericassertions.md b/docs/src/api/class-genericassertions.md index 180a15dc4b..3229dd151d 100644 --- a/docs/src/api/class-genericassertions.md +++ b/docs/src/api/class-genericassertions.md @@ -227,6 +227,30 @@ expect(value).toBeNull(); ``` +## method: GenericAssertions.toBeOneOf +* since: v1.49.1 + +Ensures that value is deeply equal to one of the elements in the expected array. + +**Usage** + +```js +const value = 2; +expect(value).toBeOneOf([1, 2, 3]); +expect(value).not.toBeOneOf([4, 5, 6]); + +const obj = { a: 1 }; +expect(obj).toBeOneOf([{ a: 1 }, { b: 2 }]); +expect(obj).not.toBeOneOf([{ a: 2 }, { b: 3 }]); +``` + +### param: GenericAssertions.toBeOneOf.expected +* since: v1.49.1 +- `expected` <[Array]<[any]>> + +Expected array to match against. + + ## method: GenericAssertions.toBeTruthy * since: v1.9 diff --git a/packages/playwright/bundles/expect/third_party/types.ts b/packages/playwright/bundles/expect/third_party/types.ts index abaa3a3b69..18ada3db62 100644 --- a/packages/playwright/bundles/expect/third_party/types.ts +++ b/packages/playwright/bundles/expect/third_party/types.ts @@ -222,6 +222,11 @@ export interface Matchers, T = unknown> { * So use `.toBeNull()` when you want to check that something is null. */ toBeNull(): R; + /** + * Use when you don't care what a value is, you just want to ensure a value + * is not null. You will often see it in tests for checking that a callback + */ + toBeOneOf(expected: Array): R; /** * Use when you don't care what a value is, you just want to ensure a value * is true in a boolean context. In JavaScript, there are six falsy values: diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts index 8d05bdafef..b463e80429 100644 --- a/packages/playwright/types/test.d.ts +++ b/packages/playwright/types/test.d.ts @@ -7040,6 +7040,24 @@ interface GenericAssertions { * */ toBeNull(): R; + /** + * Ensures that value is deeply equal to one of the elements in the expected array. + * + * **Usage** + * + * ```js + * const value = 2; + * expect(value).toBeOneOf([1, 2, 3]); + * expect(value).not.toBeOneOf([4, 5, 6]); + * + * const obj = { a: 1 }; + * expect(obj).toBeOneOf([{ a: 1 }, { b: 2 }]); + * expect(obj).not.toBeOneOf([{ a: 2 }, { b: 3 }]); + * ``` + * + * @param expected Expected array to match against. + */ + toBeOneOf(expected: unknown[]): R; /** * 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. diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts index 5775917382..fdc9564686 100644 --- a/utils/generate_types/overrides-test.d.ts +++ b/utils/generate_types/overrides-test.d.ts @@ -314,6 +314,7 @@ interface GenericAssertions { toBeLessThanOrEqual(expected: number | bigint): R; toBeNaN(): R; toBeNull(): R; + toBeOneOf(expected: unknown[]): R; toBeTruthy(): R; toBeUndefined(): R; toContain(expected: string): R;