テストがうごかないのを修正 (#7566)
* startServer * typeorm 0.2.32 * Fix: chartのテストがテストの並び順によっては正しく初期化されない * initTestDb
This commit is contained in:
@ -6,8 +6,11 @@ import * as childProcess from 'child_process';
|
||||
import * as http from 'http';
|
||||
import loadConfig from '../src/config/load';
|
||||
import { SIGKILL } from 'constants';
|
||||
import { createConnection, getConnection } from 'typeorm';
|
||||
import { entities } from '../src/db/postgre';
|
||||
|
||||
export const port = loadConfig().port;
|
||||
const config = loadConfig();
|
||||
export const port = config.port;
|
||||
|
||||
export const async = (fn: Function) => (done: Function) => {
|
||||
fn().then(() => {
|
||||
@ -147,6 +150,50 @@ export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProce
|
||||
};
|
||||
}
|
||||
|
||||
export async function initTestDb(justBorrow = false, initEntities?: any[]) {
|
||||
if (process.env.NODE_ENV !== 'test') throw 'NODE_ENV is not a test';
|
||||
|
||||
try {
|
||||
const conn = await getConnection();
|
||||
await conn.close();
|
||||
} catch (e) {}
|
||||
|
||||
return await createConnection({
|
||||
type: 'postgres',
|
||||
host: config.db.host,
|
||||
port: config.db.port,
|
||||
username: config.db.user,
|
||||
password: config.db.pass,
|
||||
database: config.db.db,
|
||||
synchronize: true && !justBorrow,
|
||||
dropSchema: true && !justBorrow,
|
||||
entities: initEntities || entities
|
||||
});
|
||||
}
|
||||
|
||||
export function startServer(timeout = 30 * 1000): Promise<childProcess.ChildProcess> {
|
||||
return new Promise((res, rej) => {
|
||||
const t = setTimeout(() => {
|
||||
p.kill(SIGKILL);
|
||||
rej('timeout to start');
|
||||
}, timeout);
|
||||
|
||||
const p = childProcess.spawn('node', [__dirname + '/../index.js'], {
|
||||
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
|
||||
env: { NODE_ENV: 'test', PATH: process.env.PATH }
|
||||
});
|
||||
|
||||
p.on('error', e => rej(e));
|
||||
|
||||
p.on('message', message => {
|
||||
if (message === 'ok') {
|
||||
clearTimeout(t);
|
||||
res(p);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function shutdownServer(p: childProcess.ChildProcess, timeout = 20 * 1000) {
|
||||
return new Promise((res, rej) => {
|
||||
const t = setTimeout(() => {
|
||||
|
Reference in New Issue
Block a user