From 76074cef7d0dbad94e9c2ceedf58f2d9ad6ffbc0 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 24 Jan 2022 15:40:20 +0200 Subject: [PATCH 1/3] fix(docs) clarify expect.any `expect.any` claims that it will check an object is created with a given constructor. This doesn't match the example following it since numbers are not created using the number constructor (they may be obtained with the number constructor called as a function). --- docs/ExpectAPI.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index bef99cce8ab5..9438e83fa137 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -348,18 +348,30 @@ 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 it's 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 function randocall(fn) { return fn(Math.floor(Math.random() * 6 + 1)); } +class Cat {} +function getCat(fn) { + return fn(new Cat()); +} + test('randocall calls its callback with a number', () => { const mock = jest.fn(); randocall(mock); expect(mock).toBeCalledWith(expect.any(Number)); }); + +test('randocall calls its callback with a class instance', () => { + const mock = jest.fn(); + getCat(mock); + expect(mock).toBeCalledWith(expect.any(Cat)); +}); + ``` ### `expect.arrayContaining(array)` From 050d1f1529cbde5269009a05e6e72e4192a8b819 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 24 Jan 2022 19:26:46 +0200 Subject: [PATCH 2/3] Update docs/ExpectAPI.md Co-authored-by: Simen Bekkhus --- docs/ExpectAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index 9438e83fa137..73dbbb40ee25 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -348,7 +348,7 @@ 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 or if it's a primitive that it's 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: +`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 function randocall(fn) { From 3beb1fa24219eacbdd87499cc89306ff0d2f2c11 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 24 Jan 2022 19:31:56 +0200 Subject: [PATCH 3/3] feedback --- docs/ExpectAPI.md | 19 +++++++++---------- .../versioned_docs/version-25.x/ExpectAPI.md | 13 ++++++++++++- .../versioned_docs/version-26.x/ExpectAPI.md | 13 ++++++++++++- .../versioned_docs/version-27.0/ExpectAPI.md | 13 ++++++++++++- .../versioned_docs/version-27.1/ExpectAPI.md | 13 ++++++++++++- .../versioned_docs/version-27.2/ExpectAPI.md | 13 ++++++++++++- .../versioned_docs/version-27.4/ExpectAPI.md | 13 ++++++++++++- 7 files changed, 81 insertions(+), 16 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index 73dbbb40ee25..e495a8ab34e6 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -351,27 +351,26 @@ test('map calls its argument with a non-null argument', () => { `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 -function randocall(fn) { - return fn(Math.floor(Math.random() * 6 + 1)); -} - class Cat {} function getCat(fn) { return fn(new Cat()); } -test('randocall calls its callback with a number', () => { - const mock = jest.fn(); - randocall(mock); - expect(mock).toBeCalledWith(expect.any(Number)); -}); - 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)); +} + +test('randocall calls its callback with a number', () => { + const mock = jest.fn(); + randocall(mock); + expect(mock).toBeCalledWith(expect.any(Number)); +}); ``` ### `expect.arrayContaining(array)` 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)); }