Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
prettier
  • Loading branch information
SimenB committed Jul 13, 2022
commit ba2a62f06d3b800ddaaf2607d2482d2e22375bf9
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
### Chore & Maintenance

- `[*]` Replace internal usage of `pretty-format/ConvertAnsi` with `jest-serializer-ansi-escapes` ([#12935](https://github.com/facebook/jest/pull/12935), [#13004](https://github.com/facebook/jest/pull/13004))
- `[docs]` Update spyOn docs ([#13000](https://github.com/facebook/jest/pull/13000))
- `[docs]` Update spyOn docs ([#13000](https://github.com/facebook/jest/pull/13000))

### Performance

Expand Down
14 changes: 9 additions & 5 deletions docs/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,15 @@ 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);`

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.

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 Down Expand Up @@ -516,7 +520,7 @@ test('plays video', () => {
const isPlaying = video.play();

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

Expand Down Expand Up @@ -566,15 +570,15 @@ test('plays video', () => {
const isPlaying = video.play;

expect(spy).toHaveBeenCalled();
expect(isPlaying).toBe(true);
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);
expect(audio.volume).toBe(100);
});
```

Expand Down
14 changes: 8 additions & 6 deletions website/versioned_docs/version-25.x/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,18 @@ 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).
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);`

:::

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

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 Down Expand Up @@ -506,7 +511,6 @@ test('plays video', () => {

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

});
```

Expand Down Expand Up @@ -547,7 +551,7 @@ const audio = require('./audio');
const video = require('./video');

afterEach(() => {
// restore the spy created with spyOn
// restore the spy created with spyOn
jest.restoreAllMocks();
});

Expand All @@ -557,7 +561,6 @@ test('plays video', () => {

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

});

test('plays audio', () => {
Expand All @@ -566,7 +569,6 @@ test('plays audio', () => {

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

});
```

Expand Down
9 changes: 5 additions & 4 deletions website/versioned_docs/version-26.x/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,15 @@ 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);`

:::

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

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 Down Expand Up @@ -511,7 +515,6 @@ test('plays video', () => {

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

});
```

Expand Down Expand Up @@ -562,7 +565,6 @@ test('plays video', () => {

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

});

test('plays audio', () => {
Expand All @@ -571,7 +573,6 @@ test('plays audio', () => {

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

});
```

Expand Down
9 changes: 5 additions & 4 deletions website/versioned_docs/version-27.x/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,15 @@ 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);`

:::

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

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 Down Expand Up @@ -511,7 +515,6 @@ test('plays video', () => {

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

});
```

Expand Down Expand Up @@ -562,7 +565,6 @@ test('plays video', () => {

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

});

test('plays audio', () => {
Expand All @@ -571,7 +573,6 @@ test('plays audio', () => {

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

});
```

Expand Down
9 changes: 5 additions & 4 deletions website/versioned_docs/version-28.0/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,15 @@ 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);`

:::

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

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 Down Expand Up @@ -517,7 +521,6 @@ test('plays video', () => {

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

});
```

Expand Down Expand Up @@ -568,7 +571,6 @@ test('plays video', () => {

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

});

test('plays audio', () => {
Expand All @@ -577,7 +579,6 @@ test('plays audio', () => {

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

});
```

Expand Down
11 changes: 5 additions & 6 deletions website/versioned_docs/version-28.1/JestObjectAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,11 +482,15 @@ 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);`

:::

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

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 @@ -511,14 +515,12 @@ afterEach(() => {
jest.restoreAllMocks();
});


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

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

});
```

Expand Down Expand Up @@ -563,14 +565,12 @@ 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);

});

test('plays audio', () => {
Expand All @@ -579,7 +579,6 @@ test('plays audio', () => {

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

});
```

Expand Down