title: Mock a dependency's constructor Jest
body: <p>I'm a newbie to Jest. I've managed to mock my own stuff, but seem to be stuck mocking a module. Specifically constructors.</p>
  
  <p>usage.js</p>
  
  <pre><code>const AWS = require("aws-sdk")
  cw = new AWS.CloudWatch({apiVersion: "2010-08-01"})
  ...
  function myMetrics(params) { 
    cw.putMetricData(params, function(err, data){})
  }
  </code></pre>
  
  <p>I'd like to do something like this in the tests. </p>
  
  <pre><code>const AWS = jest.mock("aws-sdk")
  class FakeMetrics {
    constructor() {}
    putMetricData(foo,callback) {
      callback(null, "yay!")
    }
  }
  
  AWS.CloudWatch = jest.fn( (props) =&gt; new FakeMetrics())
  </code></pre>
  
  <p>However when I come to use it in <code>usage.js</code> the cw is a <code>mockConstructor</code> not a <code>FakeMetrics</code></p>
  
  <p>I realise that my approach might be 'less than idiomatic' so I'd be greatful for any pointers.</p>
  
  <p>This is a minimal example <a href="https://github.com/ollyjshaw/jest_constructor_so" rel="noreferrer">https://github.com/ollyjshaw/jest_constructor_so</a></p>
  
  <p><code>npm install -g jest</code></p>
  
  <p><code>jest</code></p>