Skip to content

Commit

Permalink
fix(docs) clarify expect.any
Browse files Browse the repository at this point in the history
`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).
  • Loading branch information
benjamingr authored Jan 24, 2022
1 parent 5d483ce commit 76074ce
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion docs/ExpectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down

0 comments on commit 76074ce

Please sign in to comment.