Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

720
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda