BlinkUX / sequelize-mock

A simple mock interface specifically for testing code relying on Sequelize models
https://sequelize-mock.readthedocs.io
MIT License
139 stars 73 forks source link

Jest doesn't exit test cases automatically. #67

Open Vijay-CodeAstu opened 5 years ago

Vijay-CodeAstu commented 5 years ago

Hey folks,

here's my configuration:

  "dependencies": {
    "sequelize": "^4.41.1",
    "sequelize-mock": "^0.10.2"
  }

Here is my test case:

const createData = require("../index.js");
const SequelizeMock = require("sequelize-mock");
const dbMock = new SequelizeMock();
const mockData = require('./mock');

describe("Create Data", () => {
  it("Should create data in sequelize", async done => {
    try {
      const result = await createData(mockData);

      expect(result).toBeDefined();
    } catch (err) {
      expect(err).toBeNull();
    }
  });
});

The test case passes, but jest console returns: Jest did not exit one second after the test run has completed.

It might be something I missed from the tutorial. Any pointers?

Let me know if I missed something above, I'll share it asap.

Kareylo commented 5 years ago

Have you tried to call the done method ?

const createData = require("../index.js");
const SequelizeMock = require("sequelize-mock");
const dbMock = new SequelizeMock();
const mockData = require('./mock');

describe("Create Data", () => {
  it("Should create data in sequelize", async done => {
    try {
      const result = await createData(mockData);

      expect(result).toBeDefined();
    } catch (err) {
      expect(err).toBeNull();
    }
    done();
  });
});
ghost commented 5 years ago

can you please what is in mockData?

ghost commented 5 years ago

this is my test.js using jest jest.mock('../models/cartype', () => () => { const SequelizeMock = require("sequelize-mock"); const dbMock = new SequelizeMock(); return dbMock.define('cartype', { cartypename: 'check1' }) });

this is my sequelize model which i am trying to mock 'use strict'; module.exports = (sequelize, DataTypes) => { const cartype = sequelize.define('cartype', { cartypename: { type: DataTypes.STRING, allowNull: false, }, }, {}); cartype.associate = function(models) { // associations can be defined here cartype.hasMany(models.car, { foreignKey: 'cartypeid', as: 'cartypeid', }); }; return cartype; };

here is my decribe of jest describe("Test Sequelize Mocking", () => { it("Should get value from mock", async () => { const cartype = await cartypeController.list(); expect(cartype.cartypename).toEqual('check1'); }) })

i am getting error as below while mocking with sequelize-mock

NODE_ENV=test jest

FAIL server/test/function.test.js ● Test suite failed to run

car.belongsTo called with something that's not a subclass of Sequelize.Model

  32 |       onDelete: 'CASCADE',
  33 |     }),
> 34 |     car.belongsTo(models.cartype, {
     |         ^
  35 |       foreignKey: 'cartypeid',
  36 |       onDelete: 'CASCADE',
  37 |     }),

  at Function.<anonymous> (node_modules/sequelize/lib/associations/mixin.js:93:13)
  at Function.belongsTo (server/models/car.js:34:9)
  at associate (server/models/index.js:30:19)
      at Array.forEach (<anonymous>)
  at Object.forEach (server/models/index.js:28:17)
  at Object.require (server/controllers/cartypeController.js:1:17)
  at Object.require (server/test/function.test.js:2:27)

Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 8.89s Ran all test suites. Note: This command was run via npm module 'win-node-env' npm ERR! Test failed. See above for more details.

anyone able to help please do Thanks in advance

aadi009 commented 4 years ago

I am also getting the same type of error.

anthony-baru commented 3 years ago

You will have to jest.mock you car model the same way you did cartype model.