Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs) clarify expect.any #12266

Merged
merged 3 commits into from Jan 24, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/ExpectAPI.md
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:
benjamingr marked this conversation as resolved.
Show resolved Hide resolved

```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