Skip to content

efiShtain/jest-sequelize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

jest-sequelize

Drop in for mocking sequelize models

Install

npm install @efi.shtain/jest-sequelize

Usage

Best way to use this module is through a jest.setup file.

  • In your jest config, add a sequelize-mock.js file to the setupFilesAfterEnv array (You can use an existing file if you already have one)
  • Paste the following lines to the top of the file:
jest.mock("./models", () => {
  const path = require("path");
  const jestSequelize = require("@efi.shtain/jest-sequelize");
  const modelsPath = path.join(process.cwd(), "./models");
  return jestSequelize(modelsPath);
});
module.exports = async () => {};
  • All set! Happy jesting!

Test Example

// Import the relevant models
const { User } = require("./models");

describe("users service", () => {
  describe("find all", () => {
    it("should return empty array on find all", async () => {
      
      // Prepare
      const find = jest
        .spyOn(User, "find") // You can spy on any method provided
        .mockResolvedValue(Promise.resolve([])); // mock return value

      // Execute
      const users = await service.findAll(); // assuming service is under test

      // Assert
      expect(find).toBeCalled();
      expect(users).toEqual([]);
    });
  })
})

About

Drop in for mocking sequelize models

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published