Sequelize

The mock class for the base Sequelize interface.

new Sequelize([database], [username], [password], [options])

Sequelize Mock Object. This can be initialize much the same way that Sequelize itself is initialized. Any configuration or options is ignored, so it can be used as a drop-in replacement for Sequelize but does not have all the same functionality or features.

Parameters

Name Type Description
[database] String Ignored for Mock objects, supported to match Sequelize
[username] String Ignored for Mock objects, supported to match Sequelize
[password] String Ignored for Mock objects, supported to match Sequelize
[options] String Options object. Most default Sequelize options are ignored unless listed below. All, however, are available by accessing sequelize.options
[options.dialect='mock'] String Dialect that the system will use. Avaible to be returned by getDialect() but has no other effect
[options.autoQueryFallback] Boolean Flag inherited by defined Models indicating if we should try and generate results based on the query automatically
[options.stopPropagation] Boolean Flag inherited by defined Models indicating if we should not propagate to the parent

.options

Options passed into the Sequelize initialization

.importCache

Used to cache and override model imports for easy mock model importing

.models

Models that have been defined in this Sequelize Mock instances

.version

Version number for the Mock library

Sequelize

Reference to the mock Sequelize class

Utils

Reference to the Util functions

Promise

Reference to the bluebird promise library

QueryTypes

Object containing all of the Sequelize QueryTypes.

Model

Reference to the mock Model class

Instance

Reference to the mock Instance class

$queueResult(result) -> Sequelize

Queue a new query result to be returned by either the query method call or as a fallback from queries from Models defined through the define method.
Alias $queueQueryResult, $qqr

See

Parameters

Name Type Description
result Any The object or value to be returned as the result of a query

Return

Sequelize: self

$queueFailure(error, [options]) -> Sequelize

Queue a new query result to be returned by either the query method call or as a fallback from queries from Models defined through the define method. This result is returned as a rejected promise for testing error handling.
Alias $queueQueryFailure, $queueError, $queueQueryError, $qqf

See

Parameters

Name Type Description
error Any The object or value to be returned as the failure for a query
[options] Object
[options.convertNonErrors] Boolean Flag indicating if non Error objects should be allowed. Defaults to true

Return

Sequelize: self

$clearQueue() -> Sequelize

Clears any queued results from $queueResult or $queueFailure
Alias $queueClear, $queueQueryClear, $cqq, $qqc

See

Return

Sequelize: self

$overrideImport(importPath, overridePath)

Overrides a path used for import

See

Parameters

Name Type Description
importPath String The original path that import will be called with
overridePath String The path that should actually be used for resolving. If this path is relative, it will be relative to the file calling the import function

getDialect() -> String

Returns the specified dialect

Return

String: The specified dialect

getQueryInterface() -> QueryInterface

Returns the current instance of QueryInterface

See

Return

QueryInterface: The instantiated QueryInterface object used for test query

define(name, [obj={}], [opts]) -> Model

Define a new mock Model. You should provide a name and a set of default values for this new Model. The default values will be used any time a new Instance of this model is created and will be overridden by any values provided specifically to that Instance.

Additionally an options object can be passed in with an instanceMethods map. All of functions in this object will be added to any Instance of the Model that is created.

All models are available by name via the .models property

Example

sequelize.define('user', {
        'name': 'Test User',
        'email': 'test@example.com',
        'joined': new Date(),
    }, {
        'instanceMethods': {
            'tenure': function () { return Date.now() - this.get('joined'); },
        },
    });

See

  • Model

Parameters

Name Type Description
name String Name of the mock Model
[obj={}] Object Map of keys and their default values that will be used when querying against this object
[opts] Object Options for the mock model
[opts.instanceMethods] Object Map of function names and the functions to be run. These functions will be added to any instances of this Model type

Return

Model: Mock Model as defined by the name, default values, and options provided

isDefined(name) -> Boolean

Checks whether a model with the given name is defined.

Uses the .models property for lookup.

Parameters

Name Type Description
name String Name of the model

Return

Boolean: True if the model is defined, false otherwise

import(path) -> Any

Imports a given model from the provided file path. Files that are imported should export a function that accepts two parameters, this sequelize instance, and an object with all of the available datatypes

Before importing any modules, it will remap any paths that were overridden using the $overrideImport test function. This method is most helpful when used to make the SequelizeMock framework import your mock models instead of the real ones in your test code.

Parameters

Name Type Description
path String Path of the model to import. Can be relative or absolute

Return

Any: The result of evaluating the imported file's function

model(name) -> Model

Fetch a Model which is already defined.

Uses the .models property for lookup.

Parameters

Name Type Description
name String Name of the model

Return

Model: Mock model which was defined with the specified name

query() -> Promise.<Any>

Run a mock query against the QueryInterface associated with this Sequelize instance

Return

Promise.<Any>: The next result of a query as queued to the QueryInterface

transaction([fn]) -> Promise

This function will simulate the wrapping of a set of queries in a transaction. Because Sequelize Mock does not run any actual queries, there is no difference between code run through transactions and those that aren't.

Parameters

Name Type Description
[fn] Function Optional function to run as a tranasction

Return

Promise: Promise that resolves the code is successfully run, otherwise it is rejected

literal(arg) -> Any

Simply returns the first argument passed in, unmodified.

Parameters

Name Type Description
arg Any Value to return

Return

Any: value passed in

authenticate() -> Promise

Always returns a resolved promise

Return

Promise: will always resolve as a successful authentication