agusgambina
agusgambina

Reputation: 6679

Mocking tables with sequelize-mock and Jest

I want to mock two related tables with data to be able to test a function

var User = dbMock.define('user', {
  id:1,
  username: 'myTestUsername',
  email: '[email protected]',
});

var UserTeam = dbMock.define('team', {
  idTeam: 1,
  idUser: 1,
});

var Team = dbMock.define('team', {
  name: 'Test Team',
});

and this worked for me when I only have one table, but I am not sure how to export with Jest when I want to mock multiple tables

jest.mock('../sequelize/models/users', () => () => {
  const SequelizeMock = require("sequelize-mock");
  const dbMock = new SequelizeMock();
  return dbMock.define('users', {
    id: 1,
    username: 'myTestUsername',
    email: '[email protected]',
  })
});

Upvotes: 2

Views: 2222

Answers (1)

Raniere Silva
Raniere Silva

Reputation: 2697

The example below, from https://sequelize-mock.readthedocs.io/, has a example:

// Define our Model
var UserMock = DBConnectionMock.define('users', {
        'email': '[email protected]',
        'username': 'blink',
        'picture': 'user-picture.jpg',
    }, {
        instanceMethods: {
            myTestFunc: function () {
                return 'Test User';
            },
        },
    });

// You can also associate mock models as well
var GroupMock = DBConnectionMock.define('groups', {
    'name': 'My Awesome Group',
});

UserMock.belongsTo(GroupMock);

Upvotes: 1

Related Questions