Open ghost opened 5 years ago
I am having this same issue - my mock is fine but when i run this code:
Here i keep getting location is undefined
below is how my model is setup.
const data = await this.chargingDetail.create(chargingDetailData, {
include: [
{
association: this.chargingDetail.associations.location
}
]
})
this.chargingDetail is my model - when using sequlize-mocks I keep getting the error that location is undefined.
export default {
name: 'charging_detail',
build: ChargingDetail,
associate: (chargingDetail, models) => {
chargingDetail.belongsTo(models.ChargingLocation, { as: 'location' })
chargingDetail.belongsTo(models.ChargingAccount)
},
stub: () => ({
...chargingDetailAndLocationData,
associate: (chargingDetail, models) => {
chargingDetail.belongsTo(models.ChargingLocation, { as: 'location' })
chargingDetail.belongsTo(models.ChargingAccount)
}
})
I resolved this by not using the internal API in my includes statement and instead using the Model.
EDIT: to clarify:
const data = await this.chargingDetail.create(chargingDetailData, {
include: [
{
association: {model: <ModelInstance>, as: <my alias> }
}
]
})
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
I resolved this by not using the internal API in my includes statement and instead using the Model.
EDIT: to clarify:
const data = await this.chargingDetail.create(chargingDetailData, { include: [ { association: {model: <ModelInstance>, as: <my alias> } } ] })
But I have associations for my model in my models itself I am using sequelize and node
Please format code for reading
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
Did you resolve the issue? how to query data along with association i'm also using jest with sequelize-mock
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