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

Update spyOn docs #13000

Merged
merged 7 commits into from Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
### Chore & Maintenance

- `[*]` Replace internal usage of `pretty-format/ConvertAnsi` with `jest-serializer-ansi-escapes` ([#12935](https://github.com/facebook/jest/pull/12935))
- `[docs]` Update spyOn docs ([#4828](https://github.com/facebook/jest/pull/4828))
MiguelYax marked this conversation as resolved.
Show resolved Hide resolved

### Performance

Expand Down
28 changes: 18 additions & 10 deletions docs/JestObjectAPI.md
Expand Up @@ -481,7 +481,13 @@ Determines if the given function is a mocked function.

Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).

_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
:::note
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
:::

:::tip
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
:::

Example:

Expand All @@ -500,14 +506,16 @@ Example test:
```js
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps instead of a tip, short comment could be added here explaining that this will restore state of the spy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mrazauskas, I added a comment on afterEach hook on this commit 6fa35fe

});

test('plays video', () => {
const spy = jest.spyOn(video, 'play');
const isPlaying = video.play();

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
expect(isPlaying).toBe(true);
});
```

Expand Down Expand Up @@ -547,24 +555,24 @@ Example test:
const audio = require('./audio');
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
const isPlaying = video.play;

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
expect(isPlaying).toBe(true);
});

test('plays audio', () => {
const spy = jest.spyOn(audio, 'volume', 'set'); // we pass 'set'
audio.volume = 100;

expect(spy).toHaveBeenCalled();
expect(audio.volume).toBe(100);

spy.mockRestore();
expect(audio.volume).toBe(100);
});
```

Expand Down
18 changes: 14 additions & 4 deletions website/versioned_docs/version-25.x/JestObjectAPI.md
Expand Up @@ -470,8 +470,13 @@ Determines if the given function is a mocked function.
### `jest.spyOn(object, methodName)`

Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).
:::note
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
:::

_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
:::tip
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
:::

Example:

Expand All @@ -490,14 +495,17 @@ Example test:
```js
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play');
const isPlaying = video.play();

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});
```

Expand Down Expand Up @@ -537,14 +545,17 @@ Example test:
const audio = require('./audio');
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
const isPlaying = video.play;

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});

test('plays audio', () => {
Expand All @@ -554,7 +565,6 @@ test('plays audio', () => {
expect(spy).toHaveBeenCalled();
expect(audio.volume).toBe(100);

spy.mockRestore();
});
```

Expand Down
19 changes: 15 additions & 4 deletions website/versioned_docs/version-26.x/JestObjectAPI.md
Expand Up @@ -475,7 +475,13 @@ Determines if the given function is a mocked function.

Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).

_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
:::note
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
:::

:::tip
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
:::

Example:

Expand All @@ -494,14 +500,17 @@ Example test:
```js
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play');
const isPlaying = video.play();

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});
```

Expand Down Expand Up @@ -541,14 +550,17 @@ Example test:
const audio = require('./audio');
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
const isPlaying = video.play;

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});

test('plays audio', () => {
Expand All @@ -558,7 +570,6 @@ test('plays audio', () => {
expect(spy).toHaveBeenCalled();
expect(audio.volume).toBe(100);

spy.mockRestore();
});
```

Expand Down
19 changes: 15 additions & 4 deletions website/versioned_docs/version-27.x/JestObjectAPI.md
Expand Up @@ -475,7 +475,13 @@ Determines if the given function is a mocked function.

Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).

_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
:::note
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
:::

:::tip
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
:::

Example:

Expand All @@ -494,14 +500,17 @@ Example test:
```js
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play');
const isPlaying = video.play();

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});
```

Expand Down Expand Up @@ -541,14 +550,17 @@ Example test:
const audio = require('./audio');
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
const isPlaying = video.play;

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});

test('plays audio', () => {
Expand All @@ -558,7 +570,6 @@ test('plays audio', () => {
expect(spy).toHaveBeenCalled();
expect(audio.volume).toBe(100);

spy.mockRestore();
});
```

Expand Down
19 changes: 15 additions & 4 deletions website/versioned_docs/version-28.0/JestObjectAPI.md
Expand Up @@ -481,7 +481,13 @@ Determines if the given function is a mocked function.

Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).

_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
:::note
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
:::

:::tip
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
:::

Example:

Expand All @@ -500,14 +506,17 @@ Example test:
```js
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play');
const isPlaying = video.play();

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});
```

Expand Down Expand Up @@ -547,14 +556,17 @@ Example test:
const audio = require('./audio');
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});

test('plays video', () => {
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
const isPlaying = video.play;

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});

test('plays audio', () => {
Expand All @@ -564,7 +576,6 @@ test('plays audio', () => {
expect(spy).toHaveBeenCalled();
expect(audio.volume).toBe(100);

spy.mockRestore();
});
```

Expand Down
21 changes: 17 additions & 4 deletions website/versioned_docs/version-28.1/JestObjectAPI.md
Expand Up @@ -481,7 +481,13 @@ Determines if the given function is a mocked function.

Creates a mock function similar to `jest.fn` but also tracks calls to `object[methodName]`. Returns a Jest [mock function](MockFunctionAPI.md).

_Note: By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`_
:::note
By default, `jest.spyOn` also calls the **spied** method. This is different behavior from most other test libraries. If you want to overwrite the original function, you can use `jest.spyOn(object, methodName).mockImplementation(() => customImplementation)` or `object[methodName] = jest.fn(() => customImplementation);`
:::

:::tip
Since `jest.spyOn` is a mock. You could restore the initial state calling [jest.restoreAllMocks](#jestrestoreallmocks) on [afterEach](GlobalAPI.md#aftereachfn-timeout) method.
:::

Example:

Expand All @@ -500,14 +506,18 @@ Example test:
```js
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});


test('plays video', () => {
const spy = jest.spyOn(video, 'play');
const isPlaying = video.play();

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});
```

Expand Down Expand Up @@ -547,14 +557,18 @@ Example test:
const audio = require('./audio');
const video = require('./video');

afterEach(() => {
jest.restoreAllMocks();
});


test('plays video', () => {
const spy = jest.spyOn(video, 'play', 'get'); // we pass 'get'
const isPlaying = video.play;

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);

spy.mockRestore();
});

test('plays audio', () => {
Expand All @@ -564,7 +578,6 @@ test('plays audio', () => {
expect(spy).toHaveBeenCalled();
expect(audio.volume).toBe(100);

spy.mockRestore();
});
```

Expand Down