From 5f71f86cea60d72b6f0528abe78736605224cc6b Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 24 Jan 2022 23:15:55 +0200 Subject: [PATCH] fix(docs) clarify expect.any (#12266) --- docs/ExpectAPI.md | 13 ++++++++++++- website/versioned_docs/version-25.x/ExpectAPI.md | 13 ++++++++++++- website/versioned_docs/version-26.x/ExpectAPI.md | 13 ++++++++++++- website/versioned_docs/version-27.0/ExpectAPI.md | 13 ++++++++++++- website/versioned_docs/version-27.1/ExpectAPI.md | 13 ++++++++++++- website/versioned_docs/version-27.2/ExpectAPI.md | 13 ++++++++++++- website/versioned_docs/version-27.4/ExpectAPI.md | 13 ++++++++++++- 7 files changed, 84 insertions(+), 7 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index bef99cce8ab5..e495a8ab34e6 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -348,9 +348,20 @@ test('map calls its argument with a non-null argument', () => { ### `expect.any(constructor)` -`expect.any(constructor)` matches anything that was created with the given constructor. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: +`expect.any(constructor)` matches anything that was created with the given constructor or if it's a primitive that is of the passed type. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: ```js +class Cat {} +function getCat(fn) { + return fn(new Cat()); +} + +test('randocall calls its callback with a class instance', () => { + const mock = jest.fn(); + getCat(mock); + expect(mock).toBeCalledWith(expect.any(Cat)); +}); + function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } diff --git a/website/versioned_docs/version-25.x/ExpectAPI.md b/website/versioned_docs/version-25.x/ExpectAPI.md index 44974ca9c411..3322114f3ea2 100644 --- a/website/versioned_docs/version-25.x/ExpectAPI.md +++ b/website/versioned_docs/version-25.x/ExpectAPI.md @@ -313,9 +313,20 @@ test('map calls its argument with a non-null argument', () => { ### `expect.any(constructor)` -`expect.any(constructor)` matches anything that was created with the given constructor. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: +`expect.any(constructor)` matches anything that was created with the given constructor or if it's a primitive that is of the passed type. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: ```js +class Cat {} +function getCat(fn) { + return fn(new Cat()); +} + +test('randocall calls its callback with a class instance', () => { + const mock = jest.fn(); + getCat(mock); + expect(mock).toBeCalledWith(expect.any(Cat)); +}); + function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } diff --git a/website/versioned_docs/version-26.x/ExpectAPI.md b/website/versioned_docs/version-26.x/ExpectAPI.md index a66d982bbba3..87421d0cd477 100644 --- a/website/versioned_docs/version-26.x/ExpectAPI.md +++ b/website/versioned_docs/version-26.x/ExpectAPI.md @@ -313,9 +313,20 @@ test('map calls its argument with a non-null argument', () => { ### `expect.any(constructor)` -`expect.any(constructor)` matches anything that was created with the given constructor. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: +`expect.any(constructor)` matches anything that was created with the given constructor or if it's a primitive that is of the passed type. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: ```js +class Cat {} +function getCat(fn) { + return fn(new Cat()); +} + +test('randocall calls its callback with a class instance', () => { + const mock = jest.fn(); + getCat(mock); + expect(mock).toBeCalledWith(expect.any(Cat)); +}); + function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } diff --git a/website/versioned_docs/version-27.0/ExpectAPI.md b/website/versioned_docs/version-27.0/ExpectAPI.md index dbb2337e69ca..6670b3764fa2 100644 --- a/website/versioned_docs/version-27.0/ExpectAPI.md +++ b/website/versioned_docs/version-27.0/ExpectAPI.md @@ -348,9 +348,20 @@ test('map calls its argument with a non-null argument', () => { ### `expect.any(constructor)` -`expect.any(constructor)` matches anything that was created with the given constructor. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: +`expect.any(constructor)` matches anything that was created with the given constructor or if it's a primitive that is of the passed type. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: ```js +class Cat {} +function getCat(fn) { + return fn(new Cat()); +} + +test('randocall calls its callback with a class instance', () => { + const mock = jest.fn(); + getCat(mock); + expect(mock).toBeCalledWith(expect.any(Cat)); +}); + function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } diff --git a/website/versioned_docs/version-27.1/ExpectAPI.md b/website/versioned_docs/version-27.1/ExpectAPI.md index eddf1bec562e..2c25a76592dc 100644 --- a/website/versioned_docs/version-27.1/ExpectAPI.md +++ b/website/versioned_docs/version-27.1/ExpectAPI.md @@ -348,9 +348,20 @@ test('map calls its argument with a non-null argument', () => { ### `expect.any(constructor)` -`expect.any(constructor)` matches anything that was created with the given constructor. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: +`expect.any(constructor)` matches anything that was created with the given constructor or if it's a primitive that is of the passed type. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: ```js +class Cat {} +function getCat(fn) { + return fn(new Cat()); +} + +test('randocall calls its callback with a class instance', () => { + const mock = jest.fn(); + getCat(mock); + expect(mock).toBeCalledWith(expect.any(Cat)); +}); + function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } diff --git a/website/versioned_docs/version-27.2/ExpectAPI.md b/website/versioned_docs/version-27.2/ExpectAPI.md index eddf1bec562e..2c25a76592dc 100644 --- a/website/versioned_docs/version-27.2/ExpectAPI.md +++ b/website/versioned_docs/version-27.2/ExpectAPI.md @@ -348,9 +348,20 @@ test('map calls its argument with a non-null argument', () => { ### `expect.any(constructor)` -`expect.any(constructor)` matches anything that was created with the given constructor. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: +`expect.any(constructor)` matches anything that was created with the given constructor or if it's a primitive that is of the passed type. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: ```js +class Cat {} +function getCat(fn) { + return fn(new Cat()); +} + +test('randocall calls its callback with a class instance', () => { + const mock = jest.fn(); + getCat(mock); + expect(mock).toBeCalledWith(expect.any(Cat)); +}); + function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } diff --git a/website/versioned_docs/version-27.4/ExpectAPI.md b/website/versioned_docs/version-27.4/ExpectAPI.md index bef99cce8ab5..e495a8ab34e6 100644 --- a/website/versioned_docs/version-27.4/ExpectAPI.md +++ b/website/versioned_docs/version-27.4/ExpectAPI.md @@ -348,9 +348,20 @@ test('map calls its argument with a non-null argument', () => { ### `expect.any(constructor)` -`expect.any(constructor)` matches anything that was created with the given constructor. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: +`expect.any(constructor)` matches anything that was created with the given constructor or if it's a primitive that is of the passed type. You can use it inside `toEqual` or `toBeCalledWith` instead of a literal value. For example, if you want to check that a mock function is called with a number: ```js +class Cat {} +function getCat(fn) { + return fn(new Cat()); +} + +test('randocall calls its callback with a class instance', () => { + const mock = jest.fn(); + getCat(mock); + expect(mock).toBeCalledWith(expect.any(Cat)); +}); + function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); }