Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

723
Visualizações
How to mock a typeorm custom repository

I have a custom repository class which inherits from BaseRepository exported in the typeorm-transactional-cls-hooked package which in its turn becomes a plain typeorm Repository.

In my custom repository class I'm trying to mock the create and save methods but the test execution always tries to execute those functions as if it's on a real application environment trying to access a database.

File to be tested:

import { DeepPartial } from 'typeorm'
import { BaseRepository } from 'typeorm-transactional-cls-hooked'

export class TypeOrmBaseRepository<TEntity> extends BaseRepository<TEntity> {
  createAndSave(entity?: DeepPartial<TEntity>): Promise<TEntity> {
    return this.save(entity ? this.create(entity) : this.create())
  }
}

Test:

import { TypeOrmBaseRepository } from '@infrastructure/adapter/persistence/typeorm/repository/TypeOrmBaseRepository'
import { when } from 'ts-mockito'

describe('#TypeOrmBaseRepository', () => {
  let typeOrmBaseRepository: TypeOrmBaseRepository<any>

  beforeEach(() => {
    typeOrmBaseRepository = new TypeOrmBaseRepository()
  })

  it('', () => {
    when(typeOrmBaseRepository.createAndSave()).thenResolve(() => {
      return {
        save: () => 'saved'
      }
    })
    const result = typeOrmBaseRepository.createAndSave()

    console.log('result', result)
  })
})

The error:

ConnectionNotFoundError: Connection "default" was not found.

      at ConnectionNotFoundError.TypeORMError [as constructor] (src/error/TypeORMError.ts:7:9)
      at new ConnectionNotFoundError (src/error/ConnectionNotFoundError.ts:8:9)
      at ConnectionManager.Object.<anonymous>.ConnectionManager.get (src/connection/ConnectionManager.ts:42:19)
      at Object.getManager (src/globals.ts:107:35)
      at Object.<anonymous>.exports.getEntityManagerOrTransactionManager (node_modules/typeorm-transactional-cls-hooked/src/common.ts:25:27)
      at TypeOrmBaseRepository.get (node_modules/typeorm-transactional-cls-hooked/src/patch-typeorm-repository.ts:11:14)
      at TypeOrmBaseRepository.Object.<anonymous>.Repository.create (src/repository/Repository.ts:99:21)
      at TypeOrmBaseRepository.createAndSave (src/infrastructure/adapter/persistence/typeorm/repository/TypeOrmBaseRepository.ts:6:58)
      at Object.<anonymous> (test/unit/infrastructure/adapter/persistence/typeorm/entity/executed-message/repository/TypeOrmBaseRepository.spec.ts:12:32)

How do I mock the typeorm connection or repository for this to work?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda