{"id":343731,"date":"2021-02-06T08:13:19","date_gmt":"2021-02-06T00:13:19","guid":{"rendered":"http:\/\/4563.org\/?p=343731"},"modified":"2021-02-06T08:13:19","modified_gmt":"2021-02-06T00:13:19","slug":"jest-mock-%e8%bf%94%e5%9b%9e-undefined","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=343731","title":{"rendered":"jest mock \u8fd4\u56de undefined"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  jest mock \u8fd4\u56de undefined               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lbfeng <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>client.js<\/p>\n<pre><code>module.exports = class ProductsClient {   async getById(id) {     const url = `http:\/\/localhost:3000\/api\/products\/{id}`;     const response = await fetch(url);     return await response.json();   } } <\/code><\/pre>\n<p>manager.js<\/p>\n<pre><code>const ProductsClient = require('.\/client');  module.exports = class ProductManager {   async getProductToManage(id) {     const productsClient = new ProductsClient();     const productToManage = await productsClient.getById(id)       .catch(err =&gt; alert(err));     return productToManage;   } } <\/code><\/pre>\n<p>test.js<\/p>\n<pre><code>const ProductsClient = require('.\/client'); const ProductManager = require('.\/manager');  jest.mock('.\/client');  describe('test', () =&gt; {   test('111', async () =&gt; {     const expectedProduct = {       id: 1,       name: 'football',     };     const mockGetById = jest.fn();     mockGetById.mockResolvedValue(expectedProduct);     ProductsClient.prototype.getById = mockGetById;             const productManager = new ProductManager();       const result = await productManager.getProductToManage(1);      expect(result.name).toBe('football');   }) }) <\/code><\/pre>\n<p>\u5728 test.js \u4e2d mock client \u5b8c\u5168\u6ca1\u95ee\u9898\u3002\u4e00\u65e6\u628a<code>const productsClient = new ProductsClient();<\/code>\u62ff\u5230<code>class ProductManager<\/code>\u5916\uff0c<code>getById<\/code>\u5c31\u8fd4\u56de undefined<\/p>\n<pre><code>const ProductsClient = require('.\/client');  const productsClient = new ProductsClient();  module.exports = class ProductManager {   async getProductToManage(id) {     const productToManage = await productsClient.getById(id) \/\/ undefined       .catch(err =&gt; alert(err));     return productToManage;   } }  <\/code><\/pre>\n<p>jest mock module \u6587\u6863\u91cc\u4e5f\u6ca1\u627e\u5230\u7c7b\u4f3c\u7684\u4f8b\u5b50\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5297124\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : red2dog <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             jest.mock(&#8216;.\/client&#8217;,()=&gt;{ return &#8216;\u4f60\u8981 mock \u7684\u4e1c\u897f&#8217; }); \u4f60 mock \u5565\u90fd\u6ca1\u4f20\u80af\u5b9a\u662f undefined \u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5297125\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : lbfeng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @red2dog \u4f60\u8bf4\u7684\u5b98\u65b9\u6587\u6863\u91cc\u6709\uff0c\u6211\u8bd5\u8fc7\u4e86\u6ca1\u7528\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5297126\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : lbfeng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @red2dog Calling jest.mock() with the module factory parameter https:\/\/jestjs.io\/docs\/en\/es6-class-mocks                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5297127\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KuroNekoFan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e91\u4e86\u4e00\u4e0b<br \/>\u5e94\u8be5\u662f\u4f60\u6700\u540e\u7684\u5199\u6cd5\u91cc\uff0c\u5b9e\u4f8b\u5316 ProductsClient \u7684\u65f6\u5019\uff0c&#8217;.\/client&#8217;\u8fd8\u6ca1\u88ab mock<br \/>\u8bd5\u8bd5\u628a jest.mock(&#8216;.\/client&#8217;);\u653e\u5230\u6700\u9876\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>jest mock \u8fd4\u56de unde&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\/343731"}],"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=343731"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/343731\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343731"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}