{"action":"created","comment":{"url":"https://api.github.com/repos/facebook/jest/pulls/comments/171713898","pull_request_review_id":100617265,"id":171713898,"diff_hunk":"@@ -127,6 +127,51 @@ dependent component and configuring that, but the technique is the same. In\n these cases, try to avoid the temptation to implement logic inside of any\n function that's not directly being tested.\n \n+## Mocking Modules\n+\n+Suppose we have a class that fetches users from our API. The class uses\n+[axios](https://github.com/axios/axios) to call the API then returns the `data`\n+attribute which contains all the users:\n+\n+```js\n+// users.js\n+import axios from 'axios';\n+\n+class Users {\n+  static all() {\n+    return axios.get('/users.json').then(resp => resp.data);\n+  }\n+}\n+\n+export default Users;\n+```\n+\n+Now, in order to test this method without actually hitting the API (and thus\n+creating slow and fragile tests), we can use the `jest.mock(...)` function to\n+automatically mock the axios module.\n+\n+Once we mock the module we can provide a `mockReturnValue` for `.get` that\n+returns the data we want our test to assert against. In effect, we are saying\n+that we want axios.get('/users.json') to return a fake response.\n+\n+```js\n+// users.test.js\n+import axios from 'axios';\n+import Users from './users';\n+\n+jest.mock('axios');\n+\n+test('should fetch users', () => {\n+  const resp = {data: [{name: 'Bob'}]};\n+  axios.get.mockReturnValue(Promise.resolve(resp));","path":"docs/MockFunctions.md","position":40,"original_position":40,"commit_id":"3cb59c75dddd1aabfd6a36d7d425e7aabacc5fbd","original_commit_id":"3cb59c75dddd1aabfd6a36d7d425e7aabacc5fbd","user":{"login":"SimenB","id":1404810,"avatar_url":"https://avatars1.githubusercontent.com/u/1404810?v=4","gravatar_id":"","url":"https://api.github.com/users/SimenB","html_url":"https://github.com/SimenB","followers_url":"https://api.github.com/users/SimenB/followers","following_url":"https://api.github.com/users/SimenB/following{/other_user}","gists_url":"https://api.github.com/users/SimenB/gists{/gist_id}","starred_url":"https://api.github.com/users/SimenB/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SimenB/subscriptions","organizations_url":"https://api.github.com/users/SimenB/orgs","repos_url":"https://api.github.com/users/SimenB/repos","events_url":"https://api.github.com/users/SimenB/events{/privacy}","received_events_url":"https://api.github.com/users/SimenB/received_events","type":"User","site_admin":false},"body":"FWIW we have a shorthand for it: https://facebook.github.io/jest/docs/en/mock-function-api.html#mockfnmockresolvedvaluevalue\r\n\r\nUp to you if you want to use it, but maybe mention it in a comment?","created_at":"2018-03-01T22:25:58Z","updated_at":"2018-03-01T22:25:58Z","html_url":"https://github.com/facebook/jest/pull/5648#discussion_r171713898","pull_request_url":"https://api.github.com/repos/facebook/jest/pulls/5648","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/facebook/jest/pulls/comments/171713898"},"html":{"href":"https://github.com/facebook/jest/pull/5648#discussion_r171713898"},"pull_request":{"href":"https://api.github.com/repos/facebook/jest/pulls/5648"}}},"pull_request":{"url":"https://api.github.com/repos/facebook/jest/pulls/5648","id":171136512,"html_url":"https://github.com/facebook/jest/pull/5648","diff_url":"https://github.com/facebook/jest/pull/5648.diff","patch_url":"https://github.com/facebook/jest/pull/5648.patch","issue_url":"https://api.github.com/repos/facebook/jest/issues/5648","number":5648,"state":"open","locked":false,"title":"Add some docs on using `jest.mock(...)`","user":{"login":"danawoodman","id":157695,"avatar_url":"https://avatars1.githubusercontent.com/u/157695?v=4","gravatar_id":"","url":"https://api.github.com/users/danawoodman","html_url":"https://github.com/danawoodman","followers_url":"https://api.github.com/users/danawoodman/followers","following_url":"https://api.github.com/users/danawoodman/following{/other_user}","gists_url":"https://api.github.com/users/danawoodman/gists{/gist_id}","starred_url":"https://api.github.com/users/danawoodman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danawoodman/subscriptions","organizations_url":"https://api.github.com/users/danawoodman/orgs","repos_url":"https://api.github.com/users/danawoodman/repos","events_url":"https://api.github.com/users/danawoodman/events{/privacy}","received_events_url":"https://api.github.com/users/danawoodman/received_events","type":"User","site_admin":false},"body":"## Summary\r\n\r\nI find the current docs on mocking lacking in the area of mocking an existing module. The hope is this example will show people a simple, real-world way to mock node modules (whether from npm or in their own projects). \r\n\r\nI looked through all the docs and couldn't find a clear example like this so I hope it is helpful to others!\r\n\r\n## Test plan\r\n\r\nNot relevant\r\n","created_at":"2018-02-23T22:59:08Z","updated_at":"2018-03-01T22:25:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"22cb33900297d51d117b8cafaa313622f495c6f3","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[{"id":196857093,"url":"https://api.github.com/repos/facebook/jest/labels/CLA%20Signed%20:heavy_check_mark:","name":"CLA Signed :heavy_check_mark:","color":"009900","default":false}],"milestone":null,"commits_url":"https://api.github.com/repos/facebook/jest/pulls/5648/commits","review_comments_url":"https://api.github.com/repos/facebook/jest/pulls/5648/comments","review_comment_url":"https://api.github.com/repos/facebook/jest/pulls/comments{/number}","comments_url":"https://api.github.com/repos/facebook/jest/issues/5648/comments","statuses_url":"https://api.github.com/repos/facebook/jest/statuses/3cb59c75dddd1aabfd6a36d7d425e7aabacc5fbd","head":{"label":"danawoodman:patch-1","ref":"patch-1","sha":"3cb59c75dddd1aabfd6a36d7d425e7aabacc5fbd","user":{"login":"danawoodman","id":157695,"avatar_url":"https://avatars1.githubusercontent.com/u/157695?v=4","gravatar_id":"","url":"https://api.github.com/users/danawoodman","html_url":"https://github.com/danawoodman","followers_url":"https://api.github.com/users/danawoodman/followers","following_url":"https://api.github.com/users/danawoodman/following{/other_user}","gists_url":"https://api.github.com/users/danawoodman/gists{/gist_id}","starred_url":"https://api.github.com/users/danawoodman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danawoodman/subscriptions","organizations_url":"https://api.github.com/users/danawoodman/orgs","repos_url":"https://api.github.com/users/danawoodman/repos","events_url":"https://api.github.com/users/danawoodman/events{/privacy}","received_events_url":"https://api.github.com/users/danawoodman/received_events","type":"User","site_admin":false},"repo":{"id":122680289,"name":"jest","full_name":"danawoodman/jest","owner":{"login":"danawoodman","id":157695,"avatar_url":"https://avatars1.githubusercontent.com/u/157695?v=4","gravatar_id":"","url":"https://api.github.com/users/danawoodman","html_url":"https://github.com/danawoodman","followers_url":"https://api.github.com/users/danawoodman/followers","following_url":"https://api.github.com/users/danawoodman/following{/other_user}","gists_url":"https://api.github.com/users/danawoodman/gists{/gist_id}","starred_url":"https://api.github.com/users/danawoodman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danawoodman/subscriptions","organizations_url":"https://api.github.com/users/danawoodman/orgs","repos_url":"https://api.github.com/users/danawoodman/repos","events_url":"https://api.github.com/users/danawoodman/events{/privacy}","received_events_url":"https://api.github.com/users/danawoodman/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/danawoodman/jest","description":"🃏 Delightful JavaScript Testing.","fork":true,"url":"https://api.github.com/repos/danawoodman/jest","forks_url":"https://api.github.com/repos/danawoodman/jest/forks","keys_url":"https://api.github.com/repos/danawoodman/jest/keys{/key_id}","collaborators_url":"https://api.github.com/repos/danawoodman/jest/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/danawoodman/jest/teams","hooks_url":"https://api.github.com/repos/danawoodman/jest/hooks","issue_events_url":"https://api.github.com/repos/danawoodman/jest/issues/events{/number}","events_url":"https://api.github.com/repos/danawoodman/jest/events","assignees_url":"https://api.github.com/repos/danawoodman/jest/assignees{/user}","branches_url":"https://api.github.com/repos/danawoodman/jest/branches{/branch}","tags_url":"https://api.github.com/repos/danawoodman/jest/tags","blobs_url":"https://api.github.com/repos/danawoodman/jest/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/danawoodman/jest/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/danawoodman/jest/git/refs{/sha}","trees_url":"https://api.github.com/repos/danawoodman/jest/git/trees{/sha}","statuses_url":"https://api.github.com/repos/danawoodman/jest/statuses/{sha}","languages_url":"https://api.github.com/repos/danawoodman/jest/languages","stargazers_url":"https://api.github.com/repos/danawoodman/jest/stargazers","contributors_url":"https://api.github.com/repos/danawoodman/jest/contributors","subscribers_url":"https://api.github.com/repos/danawoodman/jest/subscribers","subscription_url":"https://api.github.com/repos/danawoodman/jest/subscription","commits_url":"https://api.github.com/repos/danawoodman/jest/commits{/sha}","git_commits_url":"https://api.github.com/repos/danawoodman/jest/git/commits{/sha}","comments_url":"https://api.github.com/repos/danawoodman/jest/comments{/number}","issue_comment_url":"https://api.github.com/repos/danawoodman/jest/issues/comments{/number}","contents_url":"https://api.github.com/repos/danawoodman/jest/contents/{+path}","compare_url":"https://api.github.com/repos/danawoodman/jest/compare/{base}...{head}","merges_url":"https://api.github.com/repos/danawoodman/jest/merges","archive_url":"https://api.github.com/repos/danawoodman/jest/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/danawoodman/jest/downloads","issues_url":"https://api.github.com/repos/danawoodman/jest/issues{/number}","pulls_url":"https://api.github.com/repos/danawoodman/jest/pulls{/number}","milestones_url":"https://api.github.com/repos/danawoodman/jest/milestones{/number}","notifications_url":"https://api.github.com/repos/danawoodman/jest/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/danawoodman/jest/labels{/name}","releases_url":"https://api.github.com/repos/danawoodman/jest/releases{/id}","deployments_url":"https://api.github.com/repos/danawoodman/jest/deployments","created_at":"2018-02-23T22:42:37Z","updated_at":"2018-02-23T22:42:41Z","pushed_at":"2018-03-01T22:12:07Z","git_url":"git://github.com/danawoodman/jest.git","ssh_url":"git@github.com:danawoodman/jest.git","clone_url":"https://github.com/danawoodman/jest.git","svn_url":"https://github.com/danawoodman/jest","homepage":"https://facebook.github.io/jest/","size":38559,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"facebook:master","ref":"master","sha":"fbe05d3366769f613f99e1f5aa968429bd9bd23c","user":{"login":"facebook","id":69631,"avatar_url":"https://avatars3.githubusercontent.com/u/69631?v=4","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"repo":{"id":15062869,"name":"jest","full_name":"facebook/jest","owner":{"login":"facebook","id":69631,"avatar_url":"https://avatars3.githubusercontent.com/u/69631?v=4","gravatar_id":"","url":"https://api.github.com/users/facebook","html_url":"https://github.com/facebook","followers_url":"https://api.github.com/users/facebook/followers","following_url":"https://api.github.com/users/facebook/following{/other_user}","gists_url":"https://api.github.com/users/facebook/gists{/gist_id}","starred_url":"https://api.github.com/users/facebook/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/facebook/subscriptions","organizations_url":"https://api.github.com/users/facebook/orgs","repos_url":"https://api.github.com/users/facebook/repos","events_url":"https://api.github.com/users/facebook/events{/privacy}","received_events_url":"https://api.github.com/users/facebook/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/facebook/jest","description":"🃏 Delightful JavaScript Testing.","fork":false,"url":"https://api.github.com/repos/facebook/jest","forks_url":"https://api.github.com/repos/facebook/jest/forks","keys_url":"https://api.github.com/repos/facebook/jest/keys{/key_id}","collaborators_url":"https://api.github.com/repos/facebook/jest/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/facebook/jest/teams","hooks_url":"https://api.github.com/repos/facebook/jest/hooks","issue_events_url":"https://api.github.com/repos/facebook/jest/issues/events{/number}","events_url":"https://api.github.com/repos/facebook/jest/events","assignees_url":"https://api.github.com/repos/facebook/jest/assignees{/user}","branches_url":"https://api.github.com/repos/facebook/jest/branches{/branch}","tags_url":"https://api.github.com/repos/facebook/jest/tags","blobs_url":"https://api.github.com/repos/facebook/jest/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/facebook/jest/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/facebook/jest/git/refs{/sha}","trees_url":"https://api.github.com/repos/facebook/jest/git/trees{/sha}","statuses_url":"https://api.github.com/repos/facebook/jest/statuses/{sha}","languages_url":"https://api.github.com/repos/facebook/jest/languages","stargazers_url":"https://api.github.com/repos/facebook/jest/stargazers","contributors_url":"https://api.github.com/repos/facebook/jest/contributors","subscribers_url":"https://api.github.com/repos/facebook/jest/subscribers","subscription_url":"https://api.github.com/repos/facebook/jest/subscription","commits_url":"https://api.github.com/repos/facebook/jest/commits{/sha}","git_commits_url":"https://api.github.com/repos/facebook/jest/git/commits{/sha}","comments_url":"https://api.github.com/repos/facebook/jest/comments{/number}","issue_comment_url":"https://api.github.com/repos/facebook/jest/issues/comments{/number}","contents_url":"https://api.github.com/repos/facebook/jest/contents/{+path}","compare_url":"https://api.github.com/repos/facebook/jest/compare/{base}...{head}","merges_url":"https://api.github.com/repos/facebook/jest/merges","archive_url":"https://api.github.com/repos/facebook/jest/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/facebook/jest/downloads","issues_url":"https://api.github.com/repos/facebook/jest/issues{/number}","pulls_url":"https://api.github.com/repos/facebook/jest/pulls{/number}","milestones_url":"https://api.github.com/repos/facebook/jest/milestones{/number}","notifications_url":"https://api.github.com/repos/facebook/jest/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/facebook/jest/labels{/name}","releases_url":"https://api.github.com/repos/facebook/jest/releases{/id}","deployments_url":"https://api.github.com/repos/facebook/jest/deployments","created_at":"2013-12-10T00:18:04Z","updated_at":"2018-03-01T22:07:42Z","pushed_at":"2018-03-01T22:12:09Z","git_url":"git://github.com/facebook/jest.git","ssh_url":"git@github.com:facebook/jest.git","clone_url":"https://github.com/facebook/jest.git","svn_url":"https://github.com/facebook/jest","homepage":"https://facebook.github.io/jest/","size":38990,"stargazers_count":16000,"watchers_count":16000,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":2066,"mirror_url":null,"archived":false,"open_issues_count":238,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":2066,"open_issues":238,"watchers":16000,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/facebook/jest/pulls/5648"},"html":{"href":"https://github.com/facebook/jest/pull/5648"},"issue":{"href":"https://api.github.com/repos/facebook/jest/issues/5648"},"comments":{"href":"https://api.github.com/repos/facebook/jest/issues/5648/comments"},"review_comments":{"href":"https://api.github.com/repos/facebook/jest/pulls/5648/comments"},"review_comment":{"href":"https://api.github.com/repos/facebook/jest/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/facebook/jest/pulls/5648/commits"},"statuses":{"href":"https://api.github.com/repos/facebook/jest/statuses/3cb59c75dddd1aabfd6a36d7d425e7aabacc5fbd"}},"author_association":"NONE"}}
{"actor":{"display_login":"SimenB"}}