Sequelize-typescript with jest.js tests
See original GitHub issueVersions
- sequelize: 5.19.2
- sequelize-typescript: 1.0.0
- typescript: 3.6.3
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?
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
How to test a service built on NestJS and Sequelize
Unit tests of business logic on an application built on NestJS and Sequelize. ... import { Sequelize } from 'sequelize-typescript';
Read more >nestjs-sequelize-typescript/README.md at master - GitHub
Starter kit project made with Nest that demonstrates CRUD user, JWT authentication, CRUD posts and e2e tests. Technologies implemented: sequelize-typescript ( ...
Read more >How to test Sequelize in nestjs model - Stack Overflow
Ok, I have found the solution, you just have to add this to the provider { provide: Sequelize, useValue: sequelizeMock }.
Read more >SQL (Sequelize) | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >sequelize-typescript examples - CodeSandbox
Learn how to use sequelize-typescript by viewing and forking example apps that make use of sequelize-typescript on CodeSandbox. jmcdo29/testing-nestjsA ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Running into this same issue, using ts-jest as a preprocessor. I’ve tried with
validateOnlyflag but that does not work either.Hi, I have few words, maybe something will be useful for you:
{ 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 itvalidateOnly. The only thing that i have found annoying that if you forget to mock something (let say we addedUser.findAllcall 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 themI hope it will help you with your issue