Use sequelize-mock multiple times in one test case

I’m using a MySQL DB and Sequelize ORM, with mocha, chai and sinon modules for testing. I have a User model and createUser method, and I’m trying to use sequelize-mock to mock the user data.

The code snippet below mocks multiple records in a single mock:

const SequelizeMock = require("sequelize-mock");
const dbMock = new SequelizeMock();
const userMock = dbMock.define("users");

// Mocking multiple records in a single mock
// Reference - github.com/BlinkUX/sequelize-mock/issues/55#issuecomment-383311654

userMock.$queueResult([
  userMock.build({
    id: 1,
    userName: "Superman"
  }),
  userMock.build({
    id: 2,
    userName: "Batman"
  })
]);

However, userMock.findAll() only works once and produces varying results in the console log:

createUser test
--------------- 1 -----------------
allUsers = [{"id":1,"userName":"Superman","createdAt":"2020-01-01T09:36:48.286Z","updatedAt":"2020-01-01T09:36:48.286Z"},{"id":2,"userName":"Batman","createdAt":"2020-01-01T09:36:48.286Z","updatedAt":"2020-01-01T09:36:48.286Z"}]
--------------- 2 -----------------
allUsers = [{"id":1,"createdAt":"2020-01-01T09:43:35.030Z","updatedAt":"2020-01-01T09:43:35.030Z"}]

I have two questions:

  1. Why is the value of allUsers varying when called multiple times?
  2. How can I use the same userMock multiple times? This is needed for testing the update functionality, as I need to use findOne() and update the data of one User record (present in userMock), and then find that updated record using findOne() again.

GitHub test repo URL: https://github.com/shrirambalakrishnan/sequelize-mock-test. You can run npm test to run the tests and look at the console log.

  1. The value of allUsers is varying because sequelize-mock uses a queue to store the results. When findAll() is called, the first item in the queue is returned, and then removed from the queue. Subsequent calls to findAll() return the next item in the queue. In your case, you are using $queueResult() to add two items to the queue, and then calling findAll() twice. The first call returns both items, while the second call returns only the first item (since the second item was already removed from the queue in the first call).

  2. To use the same userMock multiple times, you can create a new instance of SequelizeMock and define the users model again. Then you can use $queueResult() to add the mock data to the queue for each instance of userMock. This will allow you to use findAll() and findOne() multiple times with the same data. Here’s an example:

const SequelizeMock = require("sequelize-mock");
const dbMock1 = new SequelizeMock();
const dbMock2 = new SequelizeMock();

const userMock1 = dbMock1.define("users");
const userMock2 = dbMock2.define("users");

// Mocking multiple records in a single mock
userMock1.$queueResult([
  userMock1.build({
    id: 1,
    userName: "Superman"
  }),
  userMock1.build({
    id: 2,
    userName: "Batman"
  })
]);

userMock2.$queueResult([
  userMock2.build({
    id: 1,
    userName: "Superman"
  }),
  userMock2.build({
    id: 2,
    userName: "Batman"
  })
]);

// Use userMock1 and userMock2 as needed