Skip to content

Commit

Permalink
Fix example code to actually run 🤦‍♂️
Browse files Browse the repository at this point in the history
  • Loading branch information
danawoodman committed Mar 1, 2018
1 parent e146c86 commit 1d63239
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions docs/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ Now, in order to test this method without actually hitting the API (and thus
creating slow and fragile tests), we can use the `jest.mock(...)` function to
automatically mock the axios module.

Once we mock the module we can spy on the `.get` method and provide a
`mockImplementation` that returns the data we want our test to assert against.
In effect, we are saying that we want axios.get('/users.json') to return a fake
response.
Once we mock the module we can provide a `mockReturnValue` for `.get` that
returns the data we want our test to assert against. In effect, we are saying
that we want axios.get('/users.json') to return a fake response.

```js
// users.test.js
Expand All @@ -163,11 +162,11 @@ import Users from './users';
jest.mock('axios');

test('should fetch users', () => {
const resp = Promise.resolve({data: [{name: 'Bob'}]});
axios.get.mockReturnValue(resp);
const resp = { data: [{ name: 'Bob' }] }
axios.get.mockReturnValue(Promise.resolve(resp)

// or you could use the follwing depending on your use case:
// axios.get.mockImpementation(() => resp)
// axios.get.mockImpementation(() => Promise.resolve(resp))

return Users.all().then(users => expect(users).toEqual(resp.data));
});
Expand Down

0 comments on commit 1d63239

Please sign in to comment.