{"id":101026,"date":"2020-05-21T18:49:31","date_gmt":"2020-05-21T10:49:31","guid":{"rendered":"http:\/\/4563.org\/?p=101026"},"modified":"2020-05-21T18:49:31","modified_gmt":"2020-05-21T10:49:31","slug":"jest-%e5%86%99-unit-test%ef%bc%8c%e5%a6%82%e4%bd%95-mock-%e5%90%8c%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e4%b8%ad%e8%a2%ab%e8%b0%83%e7%94%a8%e7%9a%84%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=101026","title":{"rendered":"Jest \u5199 unit test\uff0c\u5982\u4f55 mock \u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8c03\u7528\u7684\u51fd\u6570\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Jest \u5199 unit test\uff0c\u5982\u4f55 mock \u540c\u4e00\u4e2a\u6587\u4ef6\u4e2d\u88ab\u8c03\u7528\u7684\u51fd\u6570\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>JavaScript\/TypeScript, \u4f7f\u7528 Jest \u5199 unit test \u3002<\/p>\n<p>\u4e00\u4e2a\u6587\u4ef6\u91cc\u9762\u6709\u4e24\u51fd\u6570<\/p>\n<pre><code>function a() {   \/\/\u8c03\u7528\u4e86\u5176\u5b83 modules\/files \u91cc\u9762\u7684\u7c7b\u6216\u8005\u51fd\u6570 }  function b() {   a(); } <\/code><\/pre>\n<p>\u73b0\u5728\u6211\u7528 Jest \u6d4b\u8bd5 b\uff0c\u6211\u60f3\u628a a mock \u6389\uff0c\u8bf7\u95ee\u6709\u6ca1\u6709\u6bd4\u8f83\u597d\u7684\u65b9\u6cd5\uff1f<\/p>\n<p>\u8c22\u8c22\uff01<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1670567\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gbin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c a \u548c b \u7684\u5b9a\u4f4d\u90fd\u662f\u516c\u6709\u65b9\u6cd5\uff0c\u90a3\u4e48\u53ef\u4ee5\u6a21\u5757\u5316\u628a a, \u548c b \u90fd\u5012\u51fa\uff0c\u6d4b b \u7684\u65f6\u5019\u628a a Mock \u6389\u3002<br \/>\u5982\u679c a \u7684\u5b9a\u4f4d\u662f\u79c1\u6709\u65b9\u6cd5\uff0c\u90a3\u4e48\u4e00\u822c\u4e0d\u7528 Mock a\uff0c\u4e5f\u4e0d\u7528\u4e13\u95e8\u7ed9 a \u5199\u6d4b\u8bd5\u3002\u5355\u5143\u6d4b\u8bd5\u7684\u6700\u5c0f\u5355\u4f4d\u4e0d\u4e00\u5b9a\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u5c0f\u6a21\u5757\uff0c\u751a\u81f3\u4e00\u4e2a\u7c7b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1670568\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ufo22940268 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`js<br \/>function a() {<br \/> \/\/\u8c03\u7528\u4e86\u5176\u5b83 modules\/files \u91cc\u9762\u7684\u7c7b\u6216\u8005\u51fd\u6570<br \/>}<\/p>\n<p>a = jest.fn()<br \/>function b() {<br \/> a();<br \/>}<\/p>\n<p>expect(a).toBeCalled()<br \/>&#8220;`<\/p>\n<p>https:\/\/jestjs.io\/docs\/en\/mock-functions.html                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1670569\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ufo22940268 \u5144\u5f1f\uff0c\u6ca1\u6709\u8fd9\u4e48\u7b80\u5355\u5427\u3002\u4f60\u8fd9\u662f test \u4ee3\u7801\u548c prod \u4ee3\u7801\u5728\u540c\u4e00\u6587\u4ef6\u5185\u4e48\uff1f<br \/>xxx.js<br \/>&#8220;`javascript<br \/>function a() {<br \/> \/\/\u8c03\u7528\u4e86\u5176\u5b83 modules\/files \u91cc\u9762\u7684\u7c7b\u6216\u8005\u51fd\u6570<br \/>}<\/p>\n<p>function b() {<br \/> a();<br \/>}<br \/>&#8220;`<\/p>\n<p>xxx.test.js<br \/>&#8220;`<br \/>import {a, b} from &#8220;.\/xxx.js&#8221;;<\/p>\n<p>a = jest.fn();<br \/>&#8220;`<br \/>\u8fd9\u91cc\u662f\u4f1a\u62a5\u9519\u7684\uff0c&#8221;can&#8217;t assign to a because it is not a variable&#8221;.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1670570\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ufo22940268 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/jestjs.io\/docs\/en\/manual-mocks                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1670571\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : maichael <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/stackoverflow.com\/questions\/45111198\/how-to-mock-functions-in-the-same-module-using-jest                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1670572\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : maichael <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u89c9\u5f97 John-Philip \u7684\u7b2c\u4e00\u79cd\u65b9\u6cd5\u4f18\u96c5\u4e00\u70b9\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1670573\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiadong1994 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u89c9\u5f97\u6ca1\u6709 export \u7684\u5185\u90e8\u65b9\u6cd5\u4e0d\u7528\u76f4\u63a5\u6d4b\uff0c\u6ca1\u6709 export \u8bf4\u660e\u4ed6\u53ea\u5728\u6587\u4ef6\u5185\u90e8\u6709\u4f7f\u7528\uff0c\u901a\u8fc7\u6d4b export \u7684\u51fd\u6570\u5c31\u53ef\u4ee5\u8986\u76d6\u5230\u5185\u90e8\u51fd\u6570\u3002<br \/>\u95ee\u9898\u5728\u4e8e\u4f60\u4e3a\u4ec0\u4e48\u8981 mock a \u3002\u5982\u679c\u662f\u60f3\u901a\u8fc7 mock a \u7684\u8fd4\u56de\u503c\u6765\u8986\u76d6 b \u7684\u5206\u652f\uff0c\u90a3\u4e48\u4f60\u5e94\u8be5\u7528\u4e0d\u540c\u7684 b \u53c2\u6570\u4ee5\u53ca mock \u6389\u7684\u5916\u90e8\u8c03\u7528\u7684\u8fd4\u56de\u6765\u63a7\u5236 a \u7684\u8fd4\u56de\u503c\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Jest \u5199 unit test\uff0c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/101026"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=101026"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/101026\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101026"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}