Skip to content

Commit

Permalink
Add backend e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
version-1 committed Feb 27, 2024
1 parent ed141f9 commit 15d5bdd
Show file tree
Hide file tree
Showing 28 changed files with 1,803 additions and 218 deletions.
95 changes: 55 additions & 40 deletions .github/workflows/github-actions.yml
Expand Up @@ -2,48 +2,55 @@ name: Frontend/Backend pipeline
run-name: Frontend/Backend pipeline
on: [push]
jobs:
fe-pipeline:
name: Frontend Pipeline
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./frontend/core
steps:
- uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '20.x'
- name: Cache node_modules
id: cache-npm
uses: actions/cache@v3
env:
cache-name: cache-fe-node-modules
with:
# npm cache files are stored in `~/.npm` on Linux/macOS
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
name: List the state of node modules
continue-on-error: true
run: npm list
- name: Install dependencies
run: npm install
- name: Lint
run: npm run lint
- name: Compile
run: npm run tsc
- name: Test
run: npm run test
- name: Build
run: npm run build
# fe-pipeline:
# name: Frontend Pipeline
# runs-on: ubuntu-latest
# defaults:
# run:
# working-directory: ./frontend/core
# steps:
# - uses: actions/checkout@v4
# - name: Use Node.js
# uses: actions/setup-node@v3
# with:
# node-version: '20.x'
# - name: Cache node_modules
# id: cache-npm
# uses: actions/cache@v3
# env:
# cache-name: cache-fe-node-modules
# with:
# # npm cache files are stored in `~/.npm` on Linux/macOS
# path: ~/.npm
# key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
# restore-keys: |
# ${{ runner.os }}-build-${{ env.cache-name }}-
# ${{ runner.os }}-build-
# ${{ runner.os }}-
# - if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
# name: List the state of node modules
# continue-on-error: true
# run: npm list
# - name: Install dependencies
# run: npm install
# - name: Lint
# run: npm run lint
# - name: Compile
# run: npm run tsc
# - name: Test
# run: npm run test
# - name: Build
# run: npm run build
be-pipeline:
name: Backend Pipeline
runs-on: ubuntu-latest
env:
AUTH_SECRET: testsecret
AUTH_SUGAR: testsugar
DATABASE_USERNAME: root
DATABASE_PASSWORD: root
DATABASE_HOST: localhost
NODE_ENV: test
defaults:
run:
working-directory: ./api
Expand All @@ -70,6 +77,8 @@ jobs:
name: List the state of node modules
continue-on-error: true
run: npm list
- name: MySQL
run: which mysql
- name: Install dependencies
run: npm install
- name: Lint
Expand All @@ -80,4 +89,10 @@ jobs:
run: npm run build
- name: Test
run: npm run test

- name: E2E Test - Start Database Service
run: sudo systemctl start mysql.service
- name: E2E Test - Setup Database & Run
run: |
npm run db:test:create
npm run db:test:setup
npm run test:e2e
11 changes: 8 additions & 3 deletions api/package.json
Expand Up @@ -14,19 +14,24 @@
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"tsc": "tsc --project tsconfig.json",
"test": "jest --passWithNoTests",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"test:e2e": "NODE_ENV=test jest --config ./test/jest-e2e.json --detectOpenHandles",
"typeorm": "typeorm-ts-node-commonjs",
"migration:create": "typeorm-ts-node-commonjs migration:create",
"migration:run": "typeorm-ts-node-commonjs migration:run -d src/db/config.ts",
"migration:revert": "typeorm-ts-node-commonjs migration:revert -d src/db/config.ts",
"db:migrate": "ts-node src/db/cli/index.ts migrate",
"db:seed": "ts-node src/db/cli/index.ts seed",
"db:reset": "ts-node src/db/cli/index.ts reset",
"db:setup": "npm run db:reset && npm run db:migrate && npm run db:seed"
"db:setup": "npm run db:reset && npm run db:seed",
"db:test:create": "NODE_ENV=test ts-node src/db/cli/index.ts create",
"db:test:migrate": "NODE_ENV=test ts-node src/db/cli/index.ts migrate",
"db:test:seed": "NODE_ENV=test ts-node src/db/cli/index.ts seed",
"db:test:reset": "NODE_ENV=test ts-node src/db/cli/index.ts reset",
"db:test:setup": "npm run db:test:reset && npm run db:test:seed"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand Down
1 change: 0 additions & 1 deletion api/src/app.controller.ts
Expand Up @@ -10,7 +10,6 @@ export class AppController {
@Get()
index(): { version: string } {
const res = this.appService.index();
console.log(res);
return res;
}
}
4 changes: 3 additions & 1 deletion api/src/app.module.ts
Expand Up @@ -18,11 +18,13 @@ import { LoggerModule } from './lib/modules/logger/logger.module';

const config = appConfig();

const envFilePath = [`.env.${process.env.NODE_ENV || 'development'}`];

@Module({
imports: [
ConfigModule.forRoot({
load: [appConfig],
envFilePath: ['.env.development.local', '.env.development'],
envFilePath,
validationSchema: Joi.object({
NODE_ENV: Joi.string()
.valid('development', 'production', 'test', 'provision')
Expand Down
40 changes: 28 additions & 12 deletions api/src/db/cli/index.ts
@@ -1,52 +1,68 @@
import { DataSource } from 'typeorm';
import { NestFactory } from '@nestjs/core';
import { INestApplication } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { AppModule } from '../../app.module';
import { AppDataSource } from '../config';
import { AppDataSource, RootDBDataSource } from '../config';
import { seed } from './seeds';
import { LoggerService } from '../../lib/modules/logger/logger.service';
import { Logger } from '../../lib/modules/logger/logger.service';
import { AppModule } from '../../app.module';

interface Logger {
interface ILogger {
info(...msg: any[]);
debug(...msg: any[]);
error(...msg: any[]);
}

type Context = {
dataSource: DataSource;
logger: Logger;
app: INestApplication<any>;
logger: ILogger;
appFactory: () => Promise<INestApplication>;
};

const commands: { [key: string]: (ctx: Context) => Promise<void> } = {
create: async ({ dataSource, logger }: Context) => {
logger.info('create database: ', dataSource.options.database);
await RootDBDataSource.initialize();
await RootDBDataSource.manager.query(
'create database if not exists ' + dataSource.options.database,
);
await RootDBDataSource.close();
},
reset: async ({ dataSource, logger }: Context) => {
await dataSource.initialize();
logger.info('drop database: ', dataSource.options.database);
await dataSource.dropDatabase();
logger.info('migration run');
await dataSource.runMigrations();
await dataSource.close();
},
migrate: async ({ dataSource, logger }: Context) => {
await dataSource.initialize();
logger.info('migration run');
await dataSource.runMigrations();
await dataSource.close();
},
seed: async (context: Context) => {
const { dataSource } = context;
await dataSource.initialize();
await seed(context);
await dataSource.close();
},
seed,
};

const main = async (command: keyof typeof commands) => {
const app = await NestFactory.create(AppModule);
const logger = app.get(LoggerService).logger;
const appFactory = () => NestFactory.create(AppModule);
const logger = new Logger({ level: 'info' });

try {
logger.info(`start command: ${command}`);
await AppDataSource.initialize();
const cmd = commands[command];
if (!cmd) {
logger.error(`${cmd} is undefined`);
logger.info(`${Object.keys(commands).join(', ')} is available.`);
return;
}

await cmd({ app, dataSource: AppDataSource, logger } as Context);
await cmd({ appFactory, dataSource: AppDataSource, logger } as Context);
logger.info(`end command.`);
process.exit();
} catch (e) {
Expand Down

0 comments on commit 15d5bdd

Please sign in to comment.