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 not working with sequelize model with associations using sequelize-mock #74

Open ghost opened 5 years ago

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. (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 () 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

andrewsteadcc commented 5 years ago

From my limited knowledge you have mocked the carType model but because it is associated to car model you need to mock this aswell in the same way you have done for carType

ghost commented 5 years ago

From my limited knowledge you have mocked the carType model but because it is associated to car model you need to mock this aswell in the same way you have done for carType

I mocked it but then my car has many more association in it

ghost commented 5 years ago

From my limited knowledge you have mocked the carType model but because it is associated to car model you need to mock this aswell in the same way you have done for carType

i tried mocking car also as below const functions = require('./functions'); const cartypeController = require('../controllers/cartypeController')

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

jest.mock('../models/car', () => () => { const SequelizeMockCar = require("sequelize-mock"); const dbMockCar = new SequelizeMockCar(); return dbMockCar.define('car', { vinno: 'YUIJHK652563', woac: 'true', YOM: '2017-05-10', RTONO: 'GA-02-B-3189', noofseats: '5', rateperday: '500', isActive: 'true' }) }) describe("Test Sequelize Mocking", () => { it("Should get value from mock", async () => { const cartype = await cartypeController.list(); expect(cartype.cartypename).toEqual('check1'); }) })

but stil the test fails and i get this FAIL server/test/function.test.js ● Test suite failed to run

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

18 | onDelete: 'CASCADE', 19 | })

20 | booking.belongsTo(models.car, { | ^ 21 | foreignKey: 'carid', 22 | onDelete: 'CASCADE', 23 | }) my models of car and cartype car: 'use strict'; module.exports = (sequelize, DataTypes) => { const car = sequelize.define('car', { vinno: { type: DataTypes.STRING, allowNull: false, }, woac: { type: DataTypes.BOOLEAN, }, YOM: { type: DataTypes.DATE, }, RTONO: { type: DataTypes.STRING, allowNull: false, }, noofseats: { type: DataTypes.INTEGER, }, rateperday: { type: DataTypes.INTEGER, }, isActive: { type: DataTypes.BOOLEAN, }, }, {}); car.associate = function (models) { // associations can be defined here car.belongsTo(models.systemadmin, { foreignKey: 'systemadminid', onDelete: 'CASCADE', }), car.belongsTo(models.cartype, { foreignKey: 'cartypeid', onDelete: 'CASCADE', }), car.belongsTo(models.carmodel, { foreignKey: 'carmodelid', onDelete: 'CASCADE', }), car.belongsTo(models.carbrand, { foreignKey: 'carbrandid', onDelete: 'CASCADE', }) car.hasOne(models.booking, { foreignKey: 'carid', as: 'carid', }); }; return car; };

cartype: '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; };

any suggestions thanks in advance