サーバー起動処理を共通化

This commit is contained in:
rinsuki 2019-10-25 06:24:06 +09:00
parent 6e16c9389f
commit cc7cc56abe
6 changed files with 47 additions and 85 deletions

View file

@ -16,20 +16,12 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import * as childProcess from 'child_process';
import { async, signup, request, post } from './utils';
import { async, signup, request, post, launchServer } from './utils';
describe('API visibility', () => {
let p: childProcess.ChildProcess;
before(done => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
p.on('message', message => {
if (message === 'ok') done();
});
});
before(launchServer(g => p = g));
after(() => {
p.kill();

View file

@ -16,7 +16,7 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import * as childProcess from 'child_process';
import { async, signup, request, post, react, connectStream } from './utils';
import { async, signup, request, post, react, connectStream, launchServer } from './utils';
describe('Mute', () => {
let p: childProcess.ChildProcess;
@ -26,21 +26,11 @@ describe('Mute', () => {
let bob: any;
let carol: any;
before(done => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
p.on('message', async message => {
if (message === 'ok') {
(p.channel as any).onread = () => {};
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
carol = await signup({ username: 'carol' });
done();
}
});
});
before(launchServer(g => p = g, async () => {
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
carol = await signup({ username: 'carol' });
}));
after(() => {
p.kill();

View file

@ -16,7 +16,7 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import * as childProcess from 'child_process';
import { async, signup, request, post, uploadFile } from './utils';
import { async, signup, request, post, uploadFile, launchServer } from './utils';
import { Note } from '../src/models/entities/note';
import { initDb } from '../src/db/postgre';
@ -27,23 +27,12 @@ describe('Note', () => {
let alice: any;
let bob: any;
before(done => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
p.on('message', message => {
if (message === 'ok') {
(p.channel as any).onread = () => {};
initDb(true).then(async connection => {
Notes = connection.getRepository(Note);
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
done();
});
}
});
});
before(launchServer(g => p = g, async () => {
const connection = await initDb(true);
Notes = connection.getRepository(Note);
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
}));
after(() => {
p.kill();

View file

@ -16,7 +16,7 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import * as childProcess from 'child_process';
import { connectStream, signup, request, post } from './utils';
import { connectStream, signup, request, post, launchServer } from './utils';
import { Following } from '../built/models/entities/following';
const initDb = require('../built/db/postgre.js').initDb;
@ -24,21 +24,10 @@ describe('Streaming', () => {
let p: childProcess.ChildProcess;
let Followings: any;
beforeEach(done => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
p.on('message', message => {
if (message === 'ok') {
(p.channel as any).onread = () => {};
initDb(true).then(async (connection: any) => {
Followings = connection.getRepository(Following);
done();
});
}
});
});
beforeEach(launchServer(g => p = g, async () => {
const connection = await initDb(true);
Followings = connection.getRepository(Following);
}));
afterEach(() => {
p.kill();

View file

@ -16,7 +16,7 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import * as childProcess from 'child_process';
import { async, signup, request, post, uploadFile } from './utils';
import { async, signup, request, post, uploadFile, launchServer } from './utils';
describe('users/notes', () => {
let p: childProcess.ChildProcess;
@ -26,32 +26,20 @@ describe('users/notes', () => {
let pngNote: any;
let jpgPngNote: any;
before(done => {
p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
before(launchServer(g => p = g, async () => {
alice = await signup({ username: 'alice' });
const jpg = await uploadFile(alice, __dirname + '/resources/Lenna.jpg');
const png = await uploadFile(alice, __dirname + '/resources/Lenna.png');
jpgNote = await post(alice, {
fileIds: [jpg.id]
});
p.on('message', async message => {
if (message === 'ok') {
(p.channel as any).onread = () => {};
alice = await signup({ username: 'alice' });
const jpg = await uploadFile(alice, __dirname + '/resources/Lenna.jpg');
const png = await uploadFile(alice, __dirname + '/resources/Lenna.png');
jpgNote = await post(alice, {
fileIds: [jpg.id]
});
pngNote = await post(alice, {
fileIds: [png.id]
});
jpgPngNote = await post(alice, {
fileIds: [jpg.id, png.id]
});
done();
}
pngNote = await post(alice, {
fileIds: [png.id]
});
});
jpgPngNote = await post(alice, {
fileIds: [jpg.id, png.id]
});
}));
after(() => {
p.kill();

View file

@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as WebSocket from 'ws';
const fetch = require('node-fetch');
import * as req from 'request';
import * as childProcess from 'child_process';
export const async = (fn: Function) => (done: Function) => {
fn().then(() => {
@ -102,3 +103,16 @@ export function connectStream(user: any, channel: string, listener: (message: Re
});
});
}
export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise<void> = async () => {}) {
return (done: (err?: Error) => any) => {
const p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
callbackSpawnedProcess(p)
p.on('message', message => {
if (message === 'ok') moreProcess().then(() => done()).catch(e => done(e));
});
};
}