Versions
I'm submitting a ...
[ ] bug report
[X] feature request
Hi,
Do you have any idea, how should I mock Sequalize class in tests in this case?
I have repository
const mockGetRepository = {
findOne: jest.fn(),
};
import User from 'models/User';
jest.mock('components/DatabaseConnection', () => {
return {
__esModule: true,
default: {
addModels: (m: any) => {
return;
},
getRepository: () => mockGetRepository,
models: {User},
},
};
});
import models from 'models';
import UserRepository from 'repositories/UserRepository';
describe('UserRepository test', () => {
it('findById test - user exist', async (done) => {
mockGetRepository.findOne.mockImplementationOnce(() => {
return new models.User({id: 123});
});
const user = await UserRepository.findById(123);
expect(user).toEqual({id: 123});
done();
});
});
When I try to create a new instance of User I got an error like that
Model not initialized: User cannot be instantiated. "User" needs to be added to a Sequelize instance.
Do you have any idea, how can I fix this problem?
Hey @grzegorzCieslik95 thanks for bringing this up! You could create another sequelize instance just for testing purposes with option validateOnly: true:
new Sequelize({
validateOnly: true,
models: [__dirname + '/models'] // don't forget to add your models like this... or [User, ...]
});
With validateOnly sequelize doesn't connect to any database, but the models get initialized and can be instantiated.
Hope this helps!
Maybe it would be better to provide an option, which is exclusively defined for testing purposes like testMode: true 馃
Hi @RobinBuschmann !
Thanks for your answer. In my project, I changed ORM to typeorm, but in the future project, I will try to use sequelize again.
If sequelize with validateOnly doesn't connect to the database, I think the better name is shouldConnectToDatabase or something similar. And if you can add a better description for this key will be great, because now description If true enable validate only mode nothing tell to us IMHO.
Have a nice day :)
Closing this due to inactivity
Running into this same issue, using ts-jest as a preprocessor. I've tried with validateOnly flag but that does not work either.
Running into this same issue, using ts-jest as a preprocessor. I've tried with
validateOnlyflag but that does not work either.
Did you manage to resolve this, can't find a solution anywhere?
Running into this same issue, using ts-jest as a preprocessor. I've tried with
validateOnlyflag but that does not work either.Did you manage to resolve this, can't find a solution anywhere?
Hi,
I have few words, maybe something will be useful for you:
1) If your error is something like 'Model not initialized: User cannot be instantiated. "User" needs to be added to a Sequelize instance.' you can try to init object like this { foo:1, bar:'hello' } as User. In this case you will not be forced to add model to the Sequelize instance. That's means that you can simply mock Sequelize in the same way as regular service (jest.fn()) instead of initialize it
2) If first one is not your way, i had no issues with validateOnly. The only thing that i have found annoying that if you forget to mock something (let say we added User.findAll call inside tested service method and didn't mock it) then it will try to connected to db and you will see some thing like 'database cannot be initialize' or error message similar to this one. So you just has to find these forgotten calls and mock them
I hope it will help you with your issue
Most helpful comment
Running into this same issue, using ts-jest as a preprocessor. I've tried with
validateOnlyflag but that does not work either.