/ #Node #jest 

Mocking/stubbing ES6 classes in tests with examples of Sequelize model unit tests

Get the Sequelize ES6 cheat sheet

A cheat sheet to level up your Sequelize productivity.
If you've ever needed better Sequelize docs, this is it.

I want this

This post goes through some patterns that can be used to unit test ES6 classes.

The examples will use Jest module auto-mocking but should be portable to other module mocking libraries (eg. Proxyquire) with some modifications.

In “Using ES6 classes for Sequelize 4 models” we explored how to define Sequelize models using ES6 classes. One of the benefits of such an approach is that it allows easy unit testing, it allows you to instantiate the model without the baggage of all of Sequelize (and/or a database).

Table of Contents

We’ll be testing the following model (which is an ES6 class):

const { Model } = require('sequelize');

class MyModel extends Model {
  static init() {
    return super.init(
      // Config, see "Using ES6 classes for Sequelize 4 models"
    );
  }
  
  isAvailable (date) {
    if (!Array.isArray(this.meetings)) {
      throw new Error('meetings should be eager-loaded');
    }
    return !this.meetings.find(
      ({ startDate, endDate }) => (startDate < date && endDate > date)
    );
  }
}

module.exports = MyModel;

At the module level, we want to:

  • mock/stub out Sequelize (and the Model base class)
  • import the model

In the test:

  • Instantiate the model that we’ve defined (without crashing)
  • Set some properties on that instance
  • Run some methods
  • Assert on the output
jest.mock('sequelize');
const Model = require('./model');

test('It should not throw when passed a model containing an empty list of meetings', () => {
  const model = new Model();
  model.meetings = [];
  expect(model.isAvailable.bind(null, new Date(Date.now())).not.toThrow();
});

Alternative with Object.assign

If we’re setting more than a single instance property, using Object.assign can be easier to manage:

jest.mock('sequelize');

const Model = require('./model');

test('It should not throw when passed a model containing an empty list of meetings', () => {
  const model = Object.assign(
    new Model(),
    {
      meetings: []
    }
  );
  expect(model.isAvailable.bind(null, new Date(Date.now())).not.toThrow();
});

Using ES6 classes with Sequelize makes testing easier and more natural, sign up to the newsletter to stay in the loop for the upcoming Sequelize ES6 Cheat Sheet.

unsplash-logoElevate

Looking for a new job? Take Triplebyte’s quiz and have top tech companies pitch you!

Author

Hugo Di Francesco

A Software Engineer and recovering Frontend Engineer who is big on Node.js, queues and Vue(s). He shares practical JavaScript tips for the developer who wants to get things done on Code with Hugo. University College London (UCL), MEng Mathematical Computation Graduate.

Get the Sequelize ES6 cheat sheet

A cheat sheet to level up your Sequelize productivity. If you've ever needed better Sequelize docs, this is it.