From cc7cc56abe783ca5d902e98b637d9ccaa21216bb Mon Sep 17 00:00:00 2001 From: rinsuki <428rinsuki+git@gmail.com> Date: Fri, 25 Oct 2019 06:24:06 +0900 Subject: [PATCH] =?UTF-8?q?=20=E3=82=B5=E3=83=BC=E3=83=90=E3=83=BC?= =?UTF-8?q?=E8=B5=B7=E5=8B=95=E5=87=A6=E7=90=86=E3=82=92=E5=85=B1=E9=80=9A?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/api-visibility.ts | 12 ++---------- test/mute.ts | 22 ++++++---------------- test/note.ts | 25 +++++++------------------ test/streaming.ts | 21 +++++---------------- test/user-notes.ts | 38 +++++++++++++------------------------- test/utils.ts | 14 ++++++++++++++ 6 files changed, 47 insertions(+), 85 deletions(-) diff --git a/test/api-visibility.ts b/test/api-visibility.ts index dd6c73f622..4cf6e5a3a6 100644 --- a/test/api-visibility.ts +++ b/test/api-visibility.ts @@ -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(); diff --git a/test/mute.ts b/test/mute.ts index defb2cea3c..1b1a8d7a51 100644 --- a/test/mute.ts +++ b/test/mute.ts @@ -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(); diff --git a/test/note.ts b/test/note.ts index 316ea46ec2..3aa12b5a61 100644 --- a/test/note.ts +++ b/test/note.ts @@ -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(); diff --git a/test/streaming.ts b/test/streaming.ts index 4d47deeeda..f923914c7e 100644 --- a/test/streaming.ts +++ b/test/streaming.ts @@ -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(); diff --git a/test/user-notes.ts b/test/user-notes.ts index 6091ede51b..5094180202 100644 --- a/test/user-notes.ts +++ b/test/user-notes.ts @@ -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(); diff --git a/test/utils.ts b/test/utils.ts index f67baf0048..e9c9ca015e 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -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 = 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)); + }); + }; +}