enhance(backend): テストの高速化 (#12939)
* enhance(backend): テストの高速化 * add ls * 自動的にマージされるようなので不要 * 起動方法を揃える * fix test
This commit is contained in:
parent
618e2ba1d2
commit
35ec41fc1e
45 changed files with 563 additions and 283 deletions
32
packages/backend/test-server/.eslintrc.cjs
Normal file
32
packages/backend/test-server/.eslintrc.cjs
Normal file
|
@ -0,0 +1,32 @@
|
|||
module.exports = {
|
||||
parserOptions: {
|
||||
tsconfigRootDir: __dirname,
|
||||
project: ['./tsconfig.json'],
|
||||
},
|
||||
extends: [
|
||||
'../../shared/.eslintrc.js',
|
||||
],
|
||||
rules: {
|
||||
'import/order': ['warn', {
|
||||
'groups': ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],
|
||||
'pathGroups': [
|
||||
{
|
||||
'pattern': '@/**',
|
||||
'group': 'external',
|
||||
'position': 'after'
|
||||
}
|
||||
],
|
||||
}],
|
||||
'no-restricted-globals': [
|
||||
'error',
|
||||
{
|
||||
'name': '__dirname',
|
||||
'message': 'Not in ESModule. Use `import.meta.url` instead.'
|
||||
},
|
||||
{
|
||||
'name': '__filename',
|
||||
'message': 'Not in ESModule. Use `import.meta.url` instead.'
|
||||
}
|
||||
]
|
||||
},
|
||||
};
|
23
packages/backend/test-server/.swcrc
Normal file
23
packages/backend/test-server/.swcrc
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"$schema": "https://json.schemastore.org/swcrc",
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript",
|
||||
"dynamicImport": true,
|
||||
"decorators": true
|
||||
},
|
||||
"transform": {
|
||||
"legacyDecorator": true,
|
||||
"decoratorMetadata": true
|
||||
},
|
||||
"experimental": {
|
||||
"keepImportAssertions": true
|
||||
},
|
||||
"baseUrl": "../built",
|
||||
"paths": {
|
||||
"@/*": ["*"]
|
||||
},
|
||||
"target": "es2022"
|
||||
},
|
||||
"minify": false
|
||||
}
|
80
packages/backend/test-server/entry.ts
Normal file
80
packages/backend/test-server/entry.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
import { portToPid } from 'pid-port';
|
||||
import fkill from 'fkill';
|
||||
import Fastify from 'fastify';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { MainModule } from '@/MainModule.js';
|
||||
import { ServerService } from '@/server/ServerService.js';
|
||||
import { loadConfig } from '@/config.js';
|
||||
import { NestLogger } from '@/NestLogger.js';
|
||||
|
||||
const config = loadConfig();
|
||||
const originEnv = JSON.stringify(process.env);
|
||||
|
||||
process.env.NODE_ENV = 'test';
|
||||
|
||||
/**
|
||||
* テスト用のサーバインスタンスを起動する
|
||||
*/
|
||||
async function launch() {
|
||||
await killTestServer();
|
||||
|
||||
console.log('starting application...');
|
||||
|
||||
const app = await NestFactory.createApplicationContext(MainModule, {
|
||||
logger: new NestLogger(),
|
||||
});
|
||||
const serverService = app.get(ServerService);
|
||||
await serverService.launch();
|
||||
|
||||
await startControllerEndpoints();
|
||||
|
||||
// ジョブキューは必要な時にテストコード側で起動する
|
||||
// ジョブキューが動くとテスト結果の確認に支障が出ることがあるので意図的に動かさないでいる
|
||||
|
||||
console.log('application initialized.');
|
||||
}
|
||||
|
||||
/**
|
||||
* 既に重複したポートで待ち受けしているサーバがある場合はkillする
|
||||
*/
|
||||
async function killTestServer() {
|
||||
//
|
||||
try {
|
||||
const pid = await portToPid(config.port);
|
||||
if (pid) {
|
||||
await fkill(pid, { force: true });
|
||||
}
|
||||
} catch {
|
||||
// NOP;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 別プロセスに切り離してしまったが故に出来なくなった環境変数の書き換え等を実現するためのエンドポイントを作る
|
||||
* @param port
|
||||
*/
|
||||
async function startControllerEndpoints(port = config.port + 1000) {
|
||||
const fastify = Fastify();
|
||||
|
||||
fastify.post<{ Body: { key?: string, value?: string } }>('/env', async (req, res) => {
|
||||
console.log(req.body);
|
||||
const key = req.body['key'];
|
||||
if (!key) {
|
||||
res.code(400).send({ success: false });
|
||||
return;
|
||||
}
|
||||
|
||||
process.env[key] = req.body['value'];
|
||||
|
||||
res.code(200).send({ success: true });
|
||||
});
|
||||
|
||||
fastify.post<{ Body: { key?: string, value?: string } }>('/env-reset', async (req, res) => {
|
||||
process.env = JSON.parse(originEnv);
|
||||
res.code(200).send({ success: true });
|
||||
});
|
||||
|
||||
await fastify.listen({ port: port, host: 'localhost' });
|
||||
}
|
||||
|
||||
export default launch;
|
52
packages/backend/test-server/tsconfig.json
Normal file
52
packages/backend/test-server/tsconfig.json
Normal file
|
@ -0,0 +1,52 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"noEmitOnError": true,
|
||||
"noImplicitAny": true,
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedParameters": false,
|
||||
"noUnusedLocals": false,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"declaration": false,
|
||||
"sourceMap": true,
|
||||
"target": "ES2022",
|
||||
"module": "nodenext",
|
||||
"moduleResolution": "nodenext",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"removeComments": false,
|
||||
"noLib": false,
|
||||
"strict": true,
|
||||
"strictNullChecks": true,
|
||||
"strictPropertyInitialization": false,
|
||||
"skipLibCheck": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"rootDir": "../src",
|
||||
"baseUrl": "./",
|
||||
"paths": {
|
||||
"@/*": ["../src/*"]
|
||||
},
|
||||
"outDir": "../built-test",
|
||||
"types": [
|
||||
"node"
|
||||
],
|
||||
"typeRoots": [
|
||||
"../src/@types",
|
||||
"../node_modules/@types",
|
||||
"../node_modules"
|
||||
],
|
||||
"lib": [
|
||||
"esnext"
|
||||
]
|
||||
},
|
||||
"compileOnSave": false,
|
||||
"include": [
|
||||
"./**/*.ts",
|
||||
"../src/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"../src/**/*.test.ts"
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue