如何在beforeAll()中调用数据库连接,并在afterAll()中关闭数据库连接

时间:2019-04-12 12:06:11

标签: jestjs typeorm

我是Jest和TypeORM的新手,并且想使用typeorm和Jest开发数据库验证框架。如何在beforeAll()中调用三个数据库连接实例。

这是一个用于使用TypeORM和Jest进行数据库验证的新框架,Jest Ormconfig.json包含三个数据库的详细信息,并具有用于数据库连接性的.ts类和一个测试类。

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();
})

在每次测试之前如何将连接移至数据库-

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();
}

3 个答案:

答案 0 :(得分:2)

伪代码:

let connection;

beforeAll(){
  connection = connectionDB();
}

test() {
  //...
}

afterAll() {
  connection.close();
}

答案 1 :(得分:1)

您正在混合模式。如果您使用的是 n 连接。不要创建“默认”连接,而是在ormconfig.json中创建三个命名连接。

执行完后-在配置中,您可以使用name(在您的示例中为Hello)来查找并加载配置。


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

答案 2 :(得分:0)

如果要在每次测试之前移动连接代码,则可以使用beforeEachafterEach。您还可以构建测试结构,以便在将每个测试应用于describe范围内的测试之前。

// Applies to all tests in this file
beforeEach(() => {
  return initializeCityDatabase();
});

test('city database has Vienna', () => {
  expect(isCity('Vienna')).toBeTruthy();
});

describe('matching cities to foods', () => {
  // Applies only to tests in this describe block
  beforeEach(() => {
    return initializeFoodDatabase();
  });

  test('Vienna <3 sausage', () => {
    expect(isValidCityFoodPair('Vienna', 'Wiener Schnitzel')).toBe(true);
  });
});

来源:https://jestjs.io/docs/en/setup-teardown.html