-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jest not working with sequelize model with associations using sequelize-mock #75
Comments
I am having this same issue - my mock is fine but when i run this code:
this.chargingDetail is my model - when using sequlize-mocks I keep getting the error that location is undefined.
|
I resolved this by not using the internal API in my includes statement and instead using the Model. EDIT: to clarify:
|
i tried mocking car also as below jest.mock('../models/cartype', () => () => { jest.mock('../models/car', () => () => { but stil the test fails and i get this
my models of car and cartype cartype: any suggestions thanks in advance |
But I have associations for my model in my models itself I am using sequelize and node |
Please format code for reading |
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
The text was updated successfully, but these errors were encountered: