Sequelize-typescript with jest.js tests

See original GitHub issue

Versions

  • 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:closed
  • Created 4 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
samosaboycommented, Jul 14, 2020

Running into this same issue, using ts-jest as a preprocessor. I’ve tried with validateOnly flag but that does not work either.

2reactions
dante07577commented, Feb 23, 2021

Running into this same issue, using ts-jest as a preprocessor. I’ve tried with validateOnly flag 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found