Skip to content

Commit 97c40f5

Browse files
standbyoneselfafontcu
authored andcommitted
fix(docs): fix test in making http requests
1 parent 84fd84f commit 97c40f5

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

docs/guide/advanced/http-requests.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ import { mount, flushPromises } from '@vue/test-utils'
5151
import axios from 'axios'
5252
import PostList from './PostList.vue'
5353

54-
const fakePostList = [
54+
const mockPostList = [
5555
{ id: 1, title: 'title1' },
5656
{ id: 2, title: 'title2' }
5757
]
5858

5959
// Following lines tell Jest to mock any call to `axios.get`
60-
// and to return `fakePostList` instead
60+
// and to return `mockPostList` instead
6161
jest.mock('axios', () => ({
62-
get: jest.fn(() => fakePostList)
62+
get: jest.fn(() => mockPostList)
6363
}))
6464

6565
test('loads posts on button click', async () => {
@@ -84,7 +84,10 @@ test('loads posts on button click', async () => {
8484
})
8585
```
8686

87-
Notice how we awaited `flushPromises` and then interacted with the Component. We do so to ensure that the DOM has been updated before the assertions run.
87+
Pay attention that we added prefix `mock` to the variable `mockPostList`. If not, we will get the error: "The module factory of jest.mock() is not allowed to reference any out-of-scope variables."
88+
Additional info is located here: https://jestjs.io/docs/es6-class-mocks#calling-jestmock-with-the-module-factory-parameter.
89+
90+
Also notice how we awaited `flushPromises` and then interacted with the Component. We do so to ensure that the DOM has been updated before the assertions run.
8891

8992
:::tip Alternatives to jest.mock()
9093
There are several ways of setting mocks in Jest. The one used in the example above is the simplest. For more powerful alternatives, you might want to check out [axios-mock-adapter](https://github.com/ctimmerm/axios-mock-adapter) or [msw](https://github.com/mswjs/msw), among others.

0 commit comments

Comments
 (0)