{"action":"created","comment":{"url":"https://api.github.com/repos/facebook/jest/pulls/comments/148136857","pull_request_review_id":73334794,"id":148136857,"diff_hunk":"@@ -642,12 +642,22 @@ class ModuleMockerClass {\n   }\n \n   spyOn(object: any, methodName: any): any {\n+    if (typeof object !== 'object' && typeof object !== 'function') {\n+      throw new Error(\n+        'Cannot spyOn on a primitive value; ' + this._typeOf(object) + ' given',\n+      );","path":"packages/jest-mock/src/index.js","position":16,"original_position":16,"commit_id":"1e58ca15376c054ebaac6f78157b9141530797c0","original_commit_id":"1e58ca15376c054ebaac6f78157b9141530797c0","user":{"login":"pedrottimark","id":11862657,"avatar_url":"https://avatars1.githubusercontent.com/u/11862657?v=4","gravatar_id":"","url":"https://api.github.com/users/pedrottimark","html_url":"https://github.com/pedrottimark","followers_url":"https://api.github.com/users/pedrottimark/followers","following_url":"https://api.github.com/users/pedrottimark/following{/other_user}","gists_url":"https://api.github.com/users/pedrottimark/gists{/gist_id}","starred_url":"https://api.github.com/users/pedrottimark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pedrottimark/subscriptions","organizations_url":"https://api.github.com/users/pedrottimark/orgs","repos_url":"https://api.github.com/users/pedrottimark/repos","events_url":"https://api.github.com/users/pedrottimark/events{/privacy}","received_events_url":"https://api.github.com/users/pedrottimark/received_events","type":"User","site_admin":false},"body":"Following on suggestion by Simen, here is a possibility:\r\n\r\n```js\r\nthrow new Error(\r\n  'Cannot spyOn on a primitive value\\n' +\r\n  printWithType('Got', object, printExpected),\r\n);\r\n```\r\n\r\nIs it okay to add `jest-matcher-utils` to dependencies of this package?\r\n```js\r\nimport {printExpected, printWithType} from 'jest-matcher-utils';\r\n```\r\n\r\nSee for example:\r\n* https://github.com/facebook/jest/blob/master/packages/expect/src/matchers.js#L579-L588\r\n* https://github.com/facebook/jest/blob/master/packages/jest-matcher-utils/src/index.js#L123-L133","created_at":"2017-10-31T21:49:04Z","updated_at":"2017-10-31T21:49:04Z","html_url":"https://github.com/facebook/jest/pull/4810#discussion_r148136857","pull_request_url":"https://api.github.com/repos/facebook/jest/pulls/4810","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/facebook/jest/pulls/comments/148136857"},"html":{"href":"https://github.com/facebook/jest/pull/4810#discussion_r148136857"},"pull_request":{"href":"https://api.github.com/repos/facebook/jest/pulls/4810"}}},"pull_request":{"url":"https://api.github.com/repos/facebook/jest/pulls/4810","id":149826027,"html_url":"https://github.com/facebook/jest/pull/4810","diff_url":"https://github.com/facebook/jest/pull/4810.diff","patch_url":"https://github.com/facebook/jest/pull/4810.patch","issue_url":"https://api.github.com/repos/facebook/jest/issues/4810","number":4810,"state":"open","locked":false,"title":"Improve error messaging from \"spyOn\"","user":{"login":"mjesun","id":829269,"avatar_url":"https://avatars3.githubusercontent.com/u/829269?v=4","gravatar_id":"","url":"https://api.github.com/users/mjesun","html_url":"https://github.com/mjesun","followers_url":"https://api.github.com/users/mjesun/followers","following_url":"https://api.github.com/users/mjesun/following{/other_user}","gists_url":"https://api.github.com/users/mjesun/gists{/gist_id}","starred_url":"https://api.github.com/users/mjesun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjesun/subscriptions","organizations_url":"https://api.github.com/users/mjesun/orgs","repos_url":"https://api.github.com/users/mjesun/repos","events_url":"https://api.github.com/users/mjesun/events{/privacy}","received_events_url":"https://api.github.com/users/mjesun/received_events","type":"User","site_admin":false},"body":"While developing, I found annoying that an invalid `spyOn` returns a very cryptic error, caused by the lack of checking on `fn`; it calls `fn._isMockFunction` without checking first if `fn` is actually something. \r\n\r\n![](https://user-images.githubusercontent.com/829269/32233199-c1e14e42-be51-11e7-838c-296bc54b26c1.png)\r\n\r\nThis PR improves the messaging we give when we find these kind of errors.\r\n\r\n## Passing something that is not an `object` or a `function`:\r\n\r\n![](https://user-images.githubusercontent.com/829269/32233322-078754be-be52-11e7-8c4e-82b649149d1b.png)\r\n\r\nPrimitive keys cannot be spied because of how they work, see the [ECMA-262 specification](https://www.ecma-international.org/ecma-262/8.0/index.html#sec-getv) for more info:\r\n\r\n>  If the value is not an object, the property lookup is performed using a wrapper object appropriate for the type of the value [...]\r\n\r\n## Obtaining something that is not a `function`:\r\n\r\n![](https://user-images.githubusercontent.com/829269/32233690-e23a3b3a-be52-11e7-9d9e-0a7fb1567a3f.png)","created_at":"2017-10-31T15:49:47Z","updated_at":"2017-10-31T21:49:04Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f0b4d316afda999ddf50a8549bf84bf7a960a7da","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/facebook/jest/pulls/4810/commits","review_comments_url":"https://api.github.com/repos/facebook/jest/pulls/4810/comments","review_comment_url":"https://api.github.com/repos/facebook/jest/pulls/comments{/number}","comments_url":"https://api.github.com/repos/facebook/jest/issues/4810/comments","statuses_url":"https://api.github.com/repos/facebook/jest/statuses/1e58ca15376c054ebaac6f78157b9141530797c0","head":{"label":"mjesun:fix-spy-on","ref":"fix-spy-on","sha":"1e58ca15376c054ebaac6f78157b9141530797c0","user":{"login":"mjesun","id":829269,"avatar_url":"https://avatars3.githubusercontent.com/u/829269?v=4","gravatar_id":"","url":"https://api.github.com/users/mjesun","html_url":"https://github.com/mjesun","followers_url":"https://api.github.com/users/mjesun/followers","following_url":"https://api.github.com/users/mjesun/following{/other_user}","gists_url":"https://api.github.com/users/mjesun/gists{/gist_id}","starred_url":"https://api.github.com/users/mjesun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjesun/subscriptions","organizations_url":"https://api.github.com/users/mjesun/orgs","repos_url":"https://api.github.com/users/mjesun/repos","events_url":"https://api.github.com/users/mjesun/events{/privacy}","received_events_url":"https://api.github.com/users/mjesun/received_events","type":"User","site_admin":false},"repo":{"id":94242212,"name":"jest","full_name":"mjesun/jest","owner":{"login":"mjesun","id":829269,"avatar_url":"https://avatars3.githubusercontent.com/u/829269?v=4","gravatar_id":"","url":"https://api.github.com/users/mjesun","html_url":"https://github.com/mjesun","followers_url":"https://api.github.com/users/mjesun/followers","following_url":"https://api.github.com/users/mjesun/following{/other_user}","gists_url":"https://api.github.com/users/mjesun/gists{/gist_id}","starred_url":"https://api.github.com/users/mjesun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjesun/subscriptions","organizations_url":"https://api.github.com/users/mjesun/orgs","repos_url":"https://api.github.com/users/mjesun/repos","events_url":"https://api.github.com/users/mjesun/events{/privacy}","received_events_url":"https://api.github.com/users/mjesun/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mjesun/jest","description":"🃏 Delightful JavaScript Testing.","fork":true,"url":"https://api.github.com/repos/mjesun/jest","forks_url":"https://api.github.com/repos/mjesun/jest/forks","keys_url":"https://api.github.com/repos/mjesun/jest/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mjesun/jest/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mjesun/jest/teams","hooks_url":"https://api.github.com/repos/mjesun/jest/hooks","issue_events_url":"https://api.github.com/repos/mjesun/jest/issues/events{/number}","events_url":"https://api.github.com/repos/mjesun/jest/events","assignees_url":"https://api.github.com/repos/mjesun/jest/assignees{/user}","branches_url":"https://api.github.com/repos/mjesun/jest/branches{/branch}","tags_url":"https://api.github.com/repos/mjesun/jest/tags","blobs_url":"https://api.github.com/repos/mjesun/jest/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mjesun/jest/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mjesun/jest/git/refs{/sha}","trees_url":"https://api.github.com/repos/mjesun/jest/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mjesun/jest/statuses/{sha}","languages_url":"https://api.github.com/repos/mjesun/jest/languages","stargazers_url":"https://api.github.com/repos/mjesun/jest/stargazers","contributors_url":"https://api.github.com/repos/mjesun/jest/contributors","subscribers_url":"https://api.github.com/repos/mjesun/jest/subscribers","subscription_url":"https://api.github.com/repos/mjesun/jest/subscription","commits_url":"https://api.github.com/repos/mjesun/jest/commits{/sha}","git_commits_url":"https://api.github.com/repos/mjesun/jest/git/commits{/sha}","comments_url":"https://api.github.com/repos/mjesun/jest/comments{/number}","issue_comment_url":"https://api.github.com/repos/mjesun/jest/issues/comments{/number}","contents_url":"https://api.github.com/repos/mjesun/jest/contents/{+path}","compare_url":"https://api.github.com/repos/mjesun/jest/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mjesun/jest/merges","archive_url":"https://api.github.com/repos/mjesun/jest/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mjesun/jest/downloads","issues_url":"https://api.github.com/repos/mjesun/jest/issues{/number}","pulls_url":"https://api.github.com/repos/mjesun/jest/pulls{/number}","milestones_url":"https://api.github.com/repos/mjesun/jest/milestones{/number}","notifications_url":"https://api.github.com/repos/mjesun/jest/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mjesun/jest/labels{/name}","releases_url":"https://api.github.com/repos/mjesun/jest/releases{/id}","deployments_url":"https://api.github.com/repos/mjesun/jest/deployments","created_at":"2017-06-13T18:00:17Z","updated_at":"2017-06-13T18:00:22Z","pushed_at":"2017-10-31T17:58:17Z","git_url":"git://github.com/mjesun/jest.git","ssh_url":"git@github.com:mjesun/jest.git","clone_url":"https://github.com/mjesun/jest.git","svn_url":"https://github.com/mjesun/jest","homepage":"http://facebook.github.io/jest/","size":20290,"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,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"facebook:master","ref":"master","sha":"39bf043115a53c269122b1b07265546da5082855","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":"2017-10-31T20:51:21Z","pushed_at":"2017-10-31T19:10:23Z","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":"http://facebook.github.io/jest/","size":26135,"stargazers_count":13099,"watchers_count":13099,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1685,"mirror_url":null,"archived":false,"open_issues_count":218,"forks":1685,"open_issues":218,"watchers":13099,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/facebook/jest/pulls/4810"},"html":{"href":"https://github.com/facebook/jest/pull/4810"},"issue":{"href":"https://api.github.com/repos/facebook/jest/issues/4810"},"comments":{"href":"https://api.github.com/repos/facebook/jest/issues/4810/comments"},"review_comments":{"href":"https://api.github.com/repos/facebook/jest/pulls/4810/comments"},"review_comment":{"href":"https://api.github.com/repos/facebook/jest/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/facebook/jest/pulls/4810/commits"},"statuses":{"href":"https://api.github.com/repos/facebook/jest/statuses/1e58ca15376c054ebaac6f78157b9141530797c0"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"pedrottimark"}}