Skip to content

Commit

Permalink
CIでうごくかな (#776)
Browse files Browse the repository at this point in the history
* t

* wip
  • Loading branch information
mei23 authored Jan 7, 2021
1 parent 0444200 commit 21ceb83
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ jobs:
matrix:
node-version: [12.x, 14.x]

services:
postgres:
image: postgres:10-alpine
ports:
- 5432:5432
env:
POSTGRES_DB: test-misskey
POSTGRES_HOST_AUTH_METHOD: trust
redis:
image: redis:alpine
ports:
- 6379:6379

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
48 changes: 48 additions & 0 deletions test/launch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Tests for launce server
*
* How to run the tests:
* > TS_NODE_FILES=true npx mocha test/launch.ts --require ts-node/register
*
* To specify test:
* > TS_NODE_FILES=true npx mocha test/launch.ts --require ts-node/register -g 'test name'
*/

process.env.NODE_ENV = 'test';

import * as assert from 'assert';
import * as childProcess from 'child_process';
import { async, launchServer, api, get } from './utils';

describe('Launch server', () => {
let p: childProcess.ChildProcess;

before(launchServer(g => p = g, async () => {
}));

after(() => {
p.kill();
});

it('meta', async(async () => {
const res = await api('meta', {
});

assert.strictEqual(res.status, 200);
}));

it('GET root', async(async () => {
const res = await get('/');
assert.strictEqual(res.status, 200);
}));

it('GET docs', async(async () => {
const res = await get('/docs/ja-JP/about');
assert.strictEqual(res.status, 200);
}));

it('GET api-doc', async(async () => {
const res = await get('/api-doc');
assert.strictEqual(res.status, 200);
}));
});
50 changes: 50 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
import * as childProcess from 'child_process';
import fetch from 'node-fetch';

export const async = (fn: Function) => (done: Function) => {
fn().then(() => {
done();
}, (err: Error) => {
done(err);
});
};

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', '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));
});
};
}

export const api = async (endpoint: string, params: any, me?: any): Promise<{ body: any, status: number }> => {
const auth = me ? {
i: me.token
} : {};

const res = await fetch('http://localhost:8080/api/' + endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(Object.assign(auth, params))
});

const status = res.status;
const body = res.status !== 204 ? await res.json().catch() : null;

return {
status,
body
};
};

export const get = async (path: string): Promise<{ status: number }> => {
const res = await fetch(`http://localhost:8080${path}`, {
method: 'GET',
});

const status = res.status;

return {
status
};
};

0 comments on commit 21ceb83

Please sign in to comment.