Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

406
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!