Skip to content

Commit

Permalink
Merge pull request #80 from boostcampwm2023/setting/73-be-test-enviro…
Browse files Browse the repository at this point in the history
…nment

[Setting] BE 테스트 환경 구축
  • Loading branch information
JoonSoo-Kim authored Nov 21, 2023
2 parents 83fcce3 + e995e29 commit f684ec5
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 28 deletions.
21 changes: 20 additions & 1 deletion BE/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions BE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start": "cross-env NODE_ENV=production nest start",
"start:dev": "cross-env NODE_ENV=dev nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test": "cross-env NODE_ENV=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": "cross-env NODE_ENV=test jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand All @@ -29,9 +29,9 @@
"@nestjs/typeorm": "^10.0.0",
"@types/dotenv": "^8.2.0",
"@types/passport-jwt": "^3.0.13",
"aws-sdk": "^2.1499.0",
"bcryptjs": "^2.4.3",
"class-transformer": "^0.5.1",
"aws-sdk": "^2.1499.0",
"class-validator": "^0.14.0",
"dotenv": "^16.3.1",
"mysql2": "^3.6.3",
Expand All @@ -51,6 +51,7 @@
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"cross-env": "^7.0.3",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
Expand Down
6 changes: 5 additions & 1 deletion BE/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "dotenv/config";
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { typeORMConfig } from "./configs/typeorm.config";
Expand All @@ -8,10 +9,13 @@ import { IntroduceModule } from "./introduce/introduce.module";
import { ShapesModule } from "./shapes/shapes.module";
import { ShapesRepository } from "./shapes/shapes.repository";
import { UsersRepository } from "./users/users.repository";
import { typeORMTestConfig } from "./configs/typeorm.test.config";

@Module({
imports: [
TypeOrmModule.forRoot(typeORMConfig),
TypeOrmModule.forRoot(
process.env.NODE_ENV === "test" ? typeORMTestConfig : typeORMConfig,
),
UsersModule,
DiariesModule,
AuthModule,
Expand Down
12 changes: 0 additions & 12 deletions BE/src/configs/typeorm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,3 @@ export const typeORMConfig: TypeOrmModuleOptions = {
synchronize: false,
timezone: "+09:00",
};

export const typeORMTestConfig: TypeOrmModuleOptions = {
type: "mysql",
host: process.env.DB_HOST,
port: 3306,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.TEST_DB_NAME,
entities: ["src/**/*.entity{.ts,.js}"],
synchronize: true,
timezone: "+09:00",
};
14 changes: 14 additions & 0 deletions BE/src/configs/typeorm.test.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "dotenv/config";
import { TypeOrmModuleOptions } from "@nestjs/typeorm";

export const typeORMTestConfig: TypeOrmModuleOptions = {
type: "mysql",
host: process.env.DB_HOST,
port: 3306,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.TEST_DB_NAME,
entities: ["src/**/*.entity{.ts,.js}"],
synchronize: true,
timezone: "+09:00",
};
2 changes: 1 addition & 1 deletion BE/src/diaries/diaries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CreateDiaryDto {
content: string;

@IsString()
@Matches(RegExp("^-?d+(.d+)?,-?d+(.d+)?,-?d+(.d+)?$"), {
@Matches(RegExp(/^-?d+(.d+)?,-?d+(.d+)?,-?d+(.d+)?$/), {
message: "적절하지 않은 포인트 양식입니다",
})
point: string;
Expand Down
2 changes: 1 addition & 1 deletion BE/src/shapes/shapes.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Shape extends BaseEntity {
uuid: string;

@ManyToOne(() => User, (user) => user.userId, { nullable: false })
user: Promise<User>;
user: User;

@Column()
shapePath: string;
Expand Down
2 changes: 1 addition & 1 deletion BE/src/shapes/shapes.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ShapesRepository {

if (existingShape) return;

defaultShape.user = Promise.resolve(commonUser);
defaultShape.user = commonUser;
const shape = Shape.create(defaultShape);
await shape.save();
}),
Expand Down
4 changes: 3 additions & 1 deletion BE/src/users/users.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export class CreateUserDto {
userId: string;

@IsString()
@Matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$")
@Matches(RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/), {
message: "적절하지 않은 이메일 양식입니다.",
})
email: string;

@IsString()
Expand Down
9 changes: 7 additions & 2 deletions BE/src/users/users.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import * as bcrypt from "bcryptjs";

export class UsersRepository {
async createUser(createUserDto: CreateUserDto): Promise<User> {
const { userId, password, nickname } = createUserDto;
const { userId, password, nickname, email } = createUserDto;

const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(password, salt);
const user = User.create({ userId, password: hashedPassword, nickname });
const user = User.create({
userId,
password: hashedPassword,
nickname,
email,
});

try {
await user.save();
Expand Down
4 changes: 1 addition & 3 deletions BE/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import * as request from "supertest";
import { AppModule } from "../src/app.module";
import { TypeOrmModule } from "@nestjs/typeorm";
import { typeORMTestConfig } from "src/configs/typeorm.config";

describe("AppController (e2e)", () => {
let app: INestApplication;

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [TypeOrmModule.forRoot(typeORMTestConfig), AppModule],
imports: [AppModule],
}).compile();

app = moduleFixture.createNestApplication();
Expand Down

0 comments on commit f684ec5

Please sign in to comment.