Skip to content

Commit

Permalink
Merge pull request #383 from HyoJongPark/be/test/e2e-init
Browse files Browse the repository at this point in the history
[BE] e2e 테스트 초기 세팅
  • Loading branch information
HyoJongPark authored Jan 24, 2024
2 parents 5145cbb + 8c1e43b commit d310aba
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 29 deletions.
26 changes: 14 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v3
- name: Checkout source code.
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: '20.x'

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
- name: Install yarn
run: npm install -g yarn

- name: install dependencies
- name: install dependency
run: yarn install

- name: build frontend
run: yarn frontend build
run: yarn workspace frontend build

- name: build backend
run: yarn backend build
run: yarn workspace backend build

- name: run test
run: yarn backend test
- name: run unit test
run: yarn workspace backend test
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"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": "jest --config ./test/jest-e2e.json --runInBand"
},
"dependencies": {
"@elastic/elasticsearch": "^8.10.0",
Expand Down
14 changes: 10 additions & 4 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AuthModule } from './auth/auth.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeORMDevConfig, typeORMProdConfig } from './configs/typeorm.config';
import { typeORMDevConfig, typeORMProdConfig, typeORMTestConfig } from './configs/typeorm.config';
import { RedisModule } from '@liaoliaots/nestjs-redis';
import { redisConfig } from './configs/redis.config';
import { redisConfig, testRedisConfig } from './configs/redis.config';
import { LoggerMiddleware } from './middleware/logger.middleware';
import { DiariesModule } from './diaries/diaries.module';
import { UsersModule } from './users/users.module';
Expand All @@ -17,9 +17,15 @@ import { ImagesModule } from './images/images.module';
AuthModule,
UsersModule,
TypeOrmModule.forRoot(
process.env.NODE_ENV === 'production' ? typeORMProdConfig : typeORMDevConfig,
process.env.NODE_ENV === 'production'
? typeORMProdConfig
: process.env.NODE_ENV === 'test'
? typeORMTestConfig
: typeORMDevConfig,
),
RedisModule.forRoot({ config: redisConfig }),
RedisModule.forRoot({
config: process.env.NODE_ENV === 'test' ? testRedisConfig : redisConfig,
}),
DiariesModule,
TagsModule,
FriendsModule,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/auth/guards/jwtAuth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { JWT_EXPIRED_ERROR, JWT_EXPIRE_DATE } from '../utils/auth.constant';
import { cookieExtractor } from '../strategy/jwtAuth.strategy';
import Redis from 'ioredis';
import { JwtService } from '@nestjs/jwt';
import { redisConfig } from 'src/configs/redis.config';
import { redisConfig, testRedisConfig } from 'src/configs/redis.config';
import { ExpiredTokenException } from '../utils/customException';

@Injectable()
Expand All @@ -28,7 +28,7 @@ export class JwtAuthGuard extends AuthGuard(JWT) {
} catch (err) {
if (err.name === JWT_EXPIRED_ERROR) {
// refresh token 확인
const redis = new Redis(redisConfig);
const redis = new Redis(process.env.NODE_ENV === 'test' ? testRedisConfig : redisConfig);
const refreshTokenData = await redis.get(userJwt);

if (refreshTokenData) {
Expand Down
6 changes: 6 additions & 0 deletions backend/src/configs/redis.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export const redisConfig = {
port: Number(process.env.REDIS_PORT),
password: process.env.REDIS_PASSWORD,
};

export const testRedisConfig = {
host: process.env.TEST_REDIS_HOST,
port: Number(process.env.TEST_REDIS_PORT),
password: process.env.TEST_REDIS_PASSWORD,
};
11 changes: 11 additions & 0 deletions backend/src/configs/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ export const typeORMProdConfig: TypeOrmModuleOptions = {
database: process.env.DB_NAME,
entities: [__dirname + '/../**/*.entity.{js,ts}'],
};

export const typeORMTestConfig: TypeOrmModuleOptions = {
type: 'mysql',
host: process.env.TEST_DB_HOST,
port: Number(process.env.TEST_DB_PORT),
username: process.env.TEST_DB_USER,
password: process.env.TEST_DB_PASS,
database: process.env.TEST_DB_NAME,
entities: [__dirname + '/../**/*.entity.{js,ts}'],
synchronize: true,
};
36 changes: 27 additions & 9 deletions backend/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import { AppModule } from 'src/app.module';
import { DataSource, QueryRunner } from 'typeorm';

describe('AppController (e2e)', () => {
let app: INestApplication;
let queryRunner: QueryRunner;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
const dataSource = module.get<DataSource>(DataSource);

app = moduleFixture.createNestApplication();
app = module.createNestApplication();
queryRunner = dataSource.createQueryRunner();
await app.init();
});

it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/')
.expect(200)
.expect('Hello World!');
afterAll(async () => {
await app.close();
});

beforeEach(async () => await queryRunner.startTransaction());
afterEach(async () => await queryRunner.rollbackTransaction());

it('/ (GET)', async () => {
const response = await request(app.getHttpServer()).get('/');

expect(response.status).toEqual(404);
});

it('/diaries/:id (GET)', async () => {
const response = await request(app.getHttpServer()).get('/diaries/1');
const body = response.body;

expect(response.status).toEqual(401);
expect(body.message).toEqual('Unauthorized');
});
});
3 changes: 2 additions & 1 deletion backend/test/jest-e2e.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"roots": ["../"],
"modulePaths": ["../"],
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
Expand Down
49 changes: 49 additions & 0 deletions backend/test/js/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from 'src/app.module';
import { DataSource, QueryRunner } from 'typeorm';

describe('AppController (e2e)', () => {
let app: INestApplication;
let queryRunner: QueryRunner;

beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
const dataSource = module.get<DataSource>(DataSource);

app = module.createNestApplication();
queryRunner = dataSource.createQueryRunner();
await app.init();
});

afterAll(async () => {
await app.close();
});

beforeEach(async () => await queryRunner.startTransaction());
afterEach(async () => await queryRunner.rollbackTransaction());

it('/ (GET)', async () => {
const response = await request(app.getHttpServer()).get('/');

expect(response.status).toEqual(404);
});

it('/diaries/:id (GET)', async () => {
const response = await request(app.getHttpServer()).get('/diaries/1');
const body = response.body;

expect(response.status).toEqual(401);
expect(body.message).toEqual('Unauthorized');
});
it('/diaries/:id (GET)', async () => {
const response = await request(app.getHttpServer()).get('/diaries/1');
const body = response.body;

expect(response.status).toEqual(401);
expect(body.message).toEqual('Unauthorized');
});
});

0 comments on commit d310aba

Please sign in to comment.