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

407
Vistas
Unit Testing in ts-jest with TypeOrm entities

Am trying to write tests, but am getting error. i think i have problem with connection. "Cannot execute operation on "default" connection because connection is not yet established."

i have tests folder, and in it am having user.spec.ts and testhelper.ts

testhelper.ts

// import { Connection, createConnection } from "typeorm";
import { DataSource, DataSourceOptions } from "typeorm";

import Database from "better-sqlite3";

export class TestHelper {
  private static _instance: TestHelper;

  private constructor() {}

  public static get instance(): TestHelper {
    if (!this._instance) this._instance = new TestHelper();

    return this._instance;
  }

  private dbConnect!: DataSource;
  private testdb!: any;
  async setupTestDB() {
    this.testdb = new Database(":memory:", { verbose: console.log });

    this.dbConnect = new DataSource({
      name: "default",
      type: "better-sqlite3",
      database: ":memory:",
      entities: ["src/entity/**/*.ts"],
      synchronize: true,
    } as DataSourceOptions);
  }

  teardownTestDB() {
    this.dbConnect.destroy();
    this.testdb.close();
  }
}

user.spec.ts

import { createUser } from "../src/controllers/user.controller";
//@ts-ignore
import { TestHelper } from "./testhelper";

beforeAll(async () => {
  await TestHelper.instance.setupTestDB();
});

afterAll(() => {
  TestHelper.instance.teardownTestDB();
});

describe("User Tests", () => {
  test("should create user", async () => {
    const body = {
      firstname: "John",
      lastname: "Brut",
      email: "john@examle.com",
      password: "password123",
    };
    const res = {};
    //@ts-ignore
    const user = await createUser(body, res);
    //@ts-ignore
    expect(user.firstname).toBe("John");
    //@ts-ignore
    expect(user.lastname).toBe("Brut");
  });
});

I'm doing this first time. and am stuck on it very long time... can someone please help me with this... : (

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

try this way, testhelper.ts

  async setupTestDB() {
    this.testdb = new Database(":memory:", { verbose: console.log });

    this.dbConnect = new DataSource({
      name: "default",
      type: "better-sqlite3",
      database: ":memory:",
      entities: ["src/entity/**/*.ts"],
      synchronize: true,
    } as DataSourceOptions);
    await this.dbConnect.initialize()
  }
about 4 years ago · Juan Pablo Isaza Denunciar
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