title: Jest: How to mock a library in node_modules?
body: <p>I'm trying to write a test for code that uses node-forge. For some reason, the test hangs when I call <code>forge.md.sha256.create();</code>:</p>
  
  <pre><code>  import forge from "node-forge";
  
    const privateKey = "foo";
    const storagePin = "bar";
  
    const md = forge.md.sha256.create();
    md.update(privateKey + storagePin);
  
    const metadataKey = md.digest().toHex();
  </code></pre>
  
  <p>As a workaround, I am trying to mock the implementation of that method so that it just returns a hardcoded string:</p>
  
  <pre><code>import forge from "node-forge";
  jest.mock("node-forge");
  
  forge.mockImplementation(() =&gt; {
    return {
      md: {
        sha256: {
          create: () =&gt; {
            return {
              update: () =&gt; {},
              digest: () =&gt; {
                toHex: () =&gt; "foobar";
              }
            };
          }
        }
      }
    };
  });
  
  
  // tests
  </code></pre>
  
  <p>However, my tests keep failing:</p>
  
  <pre><code>TypeError: _nodeForge2.default.mockImplementation is not a function
  
    at Object.&lt;anonymous&gt; (src/redux/epics/authentication-epic.test.js:20:27)
        at new Promise (&lt;anonymous&gt;)
    at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
  </code></pre>
  
  <p>The weird thing is, this strategy works perfectly fine when I try to mock my own files.</p>
  
  <p>What is the proper way of mocking a 3rd party library?</p>