Socket
Socket
Sign inDemoInstall

sequelize-mocking

Package Overview
Dependencies
122
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    sequelize-mocking

A Sequelize extension to deal with mocking for tests


Version published
Weekly downloads
103
increased by13.19%
Maintainers
1
Install size
13.3 MB
Created
Weekly downloads
 

Readme

Source

sequelize-mocking

Build StatusDependency Status devDependency Status

Known Vulnerabilities

NPM NPM

Sequelize extension to deal with data-mocking for testing

  • it was tested with Sequelize 3.19.3 before 1.0.0
  • it was tested with Sequelize 4.3.1 since 1.0.0.
  • it was tested with Sequelize 5.3.0 since 2.0.0

And you have to declare in your package.json the expected sequelize version

It will use the sqlite database for mocked database, will recreate it for database.

Can be integrated with Mocha and Jasmine.

A sample of use:

/**
 * Test around the @{UserService}
 *
 * @module test/user/service
 */


'use strict';

const chai = require('chai');
const sinon = require('sinon');
const path = require('path');
const sequelizeMockingMocha = require('sequelize-mocking').sequelizeMockingMocha;

describe('User - UserService (using sequelizeMockingMocha) - ', function () {
    const Database = require('../../lib/database');
    const UserService = require('../../lib/user/service');
    const UserModel = require('../../lib/user/model');

    // Basic configuration: create a sinon sandbox for testing
    let sandbox = null;

    beforeEach(function () {
        sandbox = sinon.sandbox.create();
    });

    afterEach(function () {
        sandbox && sandbox.restore();
    });

    // Load fake data for the users
    sequelizeMockingMocha(
        Database.getInstance(),
        path.resolve(path.join(__dirname, './fake-users-database.json')),
        /* Or load array of files
        [
            path.resolve(path.join(__dirname, './fake-users-database.json')),
            path.resolve(path.join(__dirname, './fake-candy-database.json')),
        ]
        */
        { 'logging': false }
    );

    it('the service shall exist', function () {
       chai.expect(UserService).to.exist;
    });

    describe('and the method findAll shall ', function () {
        it('exist', function () {
           chai.expect(UserService.findAll).to.exist;
        });

        it('shall returns an array of user', function () {
            return UserService
                .findAll()
                .then(function (users) {
                    chai.expect(users).deep.equals([{
                        'id': 1,
                        'firstName': 'John',
                        'lastName': 'Doe',
                        'age': 25,
                        'description': null
                    }]);
                });
        });
    });

    describe('and the method find shall ', function () {
        it('exist', function () {
            chai.expect(UserService.find).to.exist;
        });

        it('shall return a user if we can', function () {
            let findByIdSpy = sandbox.spy(UserModel, 'findById');

            return UserService
                .find(1)
                .then(function (user) {
                    chai.expect(findByIdSpy.called).to.be.true;
                    chai.expect(findByIdSpy.calledOnce).to.be.true;
                    chai.expect(findByIdSpy.calledWith(1)).to.be.true;

                    chai.expect(user).deep.equals({
                        'id': 1,
                        'firstName': 'John',
                        'lastName': 'Doe',
                        'age': 25,
                        'description': null
                    });
                });
        });

        it('shall return null if not found', function () {
            return UserService
                .find(-1)
                .then(function (user) {
                    chai.expect(user).to.be.null;
                });
        });
    });
});

And the mocked data from the JSON file:

[
  {
    "model": "user",
    "data": {
      "id": 1,
      "firstName": "John",
      "lastName": "Doe",
      "age": 25,
      "description": null
    }
  }
]

Keywords

FAQs

Last updated on 13 Aug 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc