title: How can I instantiate a Sequelize model to be used in Unit Tests?
body: <p>I am trying to write unit tests for a Sequelize model. I have an instance method on the class that call's Sequelize's <code>update</code> method. Turning <code>update</code> into a mocked function is no problem. I just can't seem to figure out how to properly do a <code>X = new MyModel()</code> without some kind of error. It tells me <code>Class constructor model cannot be invoked without 'new'</code>.</p>
  
  <p>I took a look at <a href="https://sequelize-mock.readthedocs.io/en/stable/" rel="nofollow noreferrer">https://sequelize-mock.readthedocs.io/en/stable/</a> but the way it's written makes me think it's better suited for mocking the Models when testing classes that are taking advantage of a model, rather than the model itself.</p>
  
  <pre><code>const BaseUtut = sequelizeInstance.define('utut', {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    metadata: {
      type: Sequelize.JSON
    }
  });
  
  class Utut extends BaseUtut {
    async updateFromFlat(id, metadata) {
  
      // in reality, i modify metadata, and want to test things around that
      modifiedMetadata = methodThatModified(metadata);
  
      // I want to see that update is called with the right output of the modified metadata
      return await this.update(
        {
          id: id,
          metadata: modifiedMetadata
        },
        {
          where: {
            locale: metadata.locale
          }
        }
      );
    }
  }
  </code></pre>
  
  <p>So with the above code sample, if I have an insance of <code>Utut</code>, I know I can easily mock <code>update</code>. I just don't know how to initialize the class to then mock things.</p>
  
  <p>The intent of the test I want to write is simply to see that <strong>update is called with the right parameters</strong>, based on my own code that has nothing to do with Sequelize. I want to make sure my transform on the inputs are set right when <em>going to</em> Sequelize. I am not planning to check that the DB is actually updated. The only problem I am having is actually instantiating the model.</p>
  
  <p>So all I need is for code like this to work:</p>
  
  <pre><code>const instance = new Utut(); // This line doesn't work
  instance.update = Jest.fn();
  </code></pre>
  
  <p>I'm not sure why it says I need to invoke with <code>new</code>, when that is exactly what I am doing.</p>