Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call DB connection in beforeAll() and DB connection close in afterAll()

Tags:

jestjs

typeorm

I am new to Jest and TypeORM and want to develop Database validation framework using typeorm and Jest. How to call three DB connections instance in beforeAll().

This is for a new framework on for Database Validation using TypeORM and Jest Ormconfig.json has details of three database and has a .ts class for databaseconnectivity and a test class.

ormconfig.json

[{
  "name": "default",
  "type": "mysql",
  "host": "127.0.01",
  "port": 3306,
  "username": "sdf",
  "password": "uuehfldjskh",
  "database": "ifsdjh",
  "synchronize": true,
  "logging": false,
  "entities": [
    "src/entity/**/*.ts"
  ],
  "migrations": [
    "src/migration/**/*.ts"
  ],
  "subscribers": [
    "src/subscriber/**/*.ts"
  ],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }
},
  {
    "name": "hello",
    "type": "mysql",
    "host": "127.0.01",
    "port": 3306,
    "username": "weqwe",
    "password": "das",
    "database": "dsfds",
    "synchronize": true,
    "logging": false,
    "entities": [
      "src/entity/**/*.ts"
    ],
    "migrations": [
      "src/migration/**/*.ts"
    ],
    "subscribers": [
      "src/subscriber/**/*.ts"
    ],
    "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
    }
  }
]

createConnection.ts

import {createConnection, getConnectionOptions} from "typeorm";

export const createConnection = async  () => {
    const createConnectionOptions = await getConnectionOptions(process.env.NODE_ENV);
    return createConnection({...createConnectionOptions,name:"default"});
}

testClass.ts

import {Patches} from "../entity/Patches";
import {createConnection} from "../utils/createConnection";

test('Query with getRepository()', async () => {
    jest.setTimeout(100000);
    const connection = await createConnection();
    const Count = await connection.getRepository(User).count();
    console.log(Count);
    expect(Count).toEqual(32);
    await connection.close();
})

How can i move connection to database before every test -

beforeAll(){
connectionDB();
}

test()
{
   connection(db1) //connect to DB1
   /** Do operation on DB1 **/
   connection(db2) //connect to DB2
   /** Do operation on DB2 **/
   Compare both result of DB1 and DB2
}

afterAll()
{
connectionDB().close();
}
like image 555
vectyvec Avatar asked Apr 12 '19 12:04

vectyvec


2 Answers

pseudocode:

let connection;

beforeAll(){
  connection = connectionDB();
}

test() {
  //...
}

afterAll() {
  connection.close();
}
like image 54
RidgeA Avatar answered Feb 01 '23 22:02

RidgeA


You are mixing patterns. if you are using n connections. Do not create a "Default" connection, but rather three named connections in the ormconfig.json.

once you do that -- in the config you use the name (hello in your example)to find and load the configuration.


beforeEach(async () => {
  await TypeORM.createConnection('connection1Name')
  await TypeORM.createConnection('connection2Name')
  await TypeORM.createConnection('connection3Name')

})

afterEach(async () => {
  await getConnection('connection1Name').close()
  await getConnection('connection2Name').close()
  await getConnection('connection3Name').close()

})

// in your tests you can find use getConnection('name') to use the specific connection

like image 28
Jonathan Avatar answered Feb 02 '23 00:02

Jonathan