Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

sequelize-mock

BlinkUX146.8kMIT0.10.2

A simple mock interface specifically for testing code relying on Sequelize models

sequelize, mocking, test, testing

readme

Sequelize Mock

npm CircleCI Coveralls MIT License Documentation Status

A mocking interface designed for testing code that uses Sequelize.

Documentation at sequelize-mock.readthedocs.io

Install

npm i sequelize-mock --save-dev

Getting Started

The Mock Models created with this library function as drop in replacements for your unit testing.

Start by importing the library

var SequelizeMock = require('sequelize-mock');

Initialize the library as you would Sequelize

var DBConnectionMock = new SequelizeMock();

Define your models

var UserMock = DBConnectionMock.define('users', {
        'email': 'email@example.com',
        'username': 'blink',
        'picture': 'user-picture.jpg',
    }, {
        instanceMethods: {
            myTestFunc: function () {
                return 'Test User';
            },
        },
    });

Once Mock models have been defined, you can use them as drop-in replacements for your Sequelize model objects. Data is not retrieved from a database and instead is returned based on the setup of the mock objects, the query being made, and other applied or included information.

For example, your code might look something like this

UserMock.findOne({
    where: {
        username: 'my-user',
    },
}).then(function (user) {
    // `user` is a Sequelize Model-like object
    user.get('id');         // Auto-Incrementing ID available on all Models
    user.get('email');      // 'email@example.com'; Pulled from default values
    user.get('username');   // 'my-user'; Pulled from the `where` in the query

    user.myTestFunc();      // Will return 'Test User' as defined above
});

Contributing

This library is under active development, so you should feel free to submit issues, questions, or pull requests.

License

Created by Blink UX and licensed under the MIT license. Check the LICENSE file for more information.

changelog

Changelog

v0.10.2 - 8ed75d2 - Dec 4th 2017

  • Fix for DataTypes not being exposed during sequelize.import calls
  • DEV Added .editorconfig file to normalize editors and minimize whitespace changes

v0.10.1 - 43cc668 - Nov 20th 2017

  • Fix for relative file paths in the sequelize.import function

v0.10.0 - 0a7f270f - Oct 24th 2017

  • Add sequelize.import support
  • Add sequelize.$overrideImport test functionality to allow overriding imported module paths
  • Add support for Model.findAndCount() and it's alias Model.findAndCountAll() (thanks to @TerryMooreII)

v0.9.1 - 3aeaa05 - Sep 21st 2017

  • Add authenticate() to sequelize which always resolves (thanks to @vFederer)
  • Fix a few documentation issues

v0.9.0 - c75d75e - Jul 28th 2017

  • Add DataType mock objects for use with any DataType funcitonality
  • Add support for conditional query result handling (thanks to @scinos)
  • Add support for instance.get({ plain: true }) (thanks to @fredstrange)
  • Add support for sequelize.model and sequelize.isDefined (thanks to @Thylossus)
  • Fix setting isNewRecord on instances to false after saving (issue #19; thanks to @scinos)

v0.8.1 - c2527de - May 22nd 2017

  • Fix creating Associations throws an error (issue #10)

v0.8.0 - 60397ec - May 18th 2017

  • Add $queueResult(), $queueFailure(), and $clearQueue() test methods to Sequelize and Model objects
  • Add QueryInterface object to support test result mocking
  • Add getQueryInterface() to Sequelize instances which will get the associated QueryInterface object
  • Add getters/setters for accessing Instance data values via using the simple object syntax (e.g. instance.foo = 'bar')
  • Add support for hasPrimary and timestamps options on Models
  • Add instance.Model reference to the calling Model object that the instance is based on
  • Change Model.Instance can now be directly used to create a mock Instance object of the given model
  • BREAKING The query() method for Sequelize instances will now throw instead of returning a rejected Promise by default. See the $queueResult or $queueFailure methods for getting proper returns from calls to this function
  • BREAKING The Instance object should now only be instantiated by going through a Model using either model.build() or Model.Instance
  • DEV Added HTML code coverage report to default npm test run

v0.7.0 - bcfb924 - Feb 3rd 2017

  • Add Model.bulkCreate() support
  • Add Instance.set() Object parameter support
  • Add Instance.get() no parameter support to get a cloned Object of all of the values
  • Add Instance.destroy() will now set a deletedAt property to the current date
  • Add Sequelize.Utils._ which points to the lodash library
  • Add options parameter in the Sequelize constructor
  • Add getDialect() to Sequelize instances which will return the value from the options passed into the constructor
  • Add Sequelize.Instance which points to the mock Instance class
  • Change Model.destroy() will return the value of options.limit if that value exists and is a number
  • BREAKING Removed Model.generateTestModel(), Model.generateModelPromise(), and Model.generateModelsPromises()
  • DEV Added documentation generation via npm run docs-gen

v0.6.1 - d65cbc9 - Dec 7th 2016

  • Fix Instance initialization modifying the original passed in object

v0.6.0 - 4315930 - Dec 6th 2016

  • Add Sequelize Error object mocks
  • Add validate() function + calls in the appropriate places
  • Add $addValidationError(col[, message[, type]]) to Instances which will trigger a validation error on the next call to .validate() from any of places it can be called
  • Add $removeValidationError(col) to Instances which will remove any validation errors for that column
  • Add $clearValidationErrors() to Instances which removes all validation errors from all of the columns

v0.5.1 - b9a34d2 - Nov 4th 2016

  • Add Model.unscoped()
  • Add Model.insertOrUpdate() as alias for Model.upsert()
  • Fix for Sequelize.Utils.lowercaseFirst()
  • Fix Model.update() affected rows return value to be array

v0.5.0 - 3d436f7 - Oct 7th 2016

  • Initial Public Release