Skip to content

Commit 84f016c

Browse files
committed
feature: fixture 를 utils 로 독립, import 수정
1 parent 1cb8b68 commit 84f016c

File tree

8 files changed

+88
-87
lines changed

8 files changed

+88
-87
lines changed

src/middlewares/__test__/auth.middleware.test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Request, Response } from 'express';
22
import { authMiddleware } from '@/middlewares/auth.middleware';
33
import pool from '@/configs/db.config';
4+
import { mockUser } from '@/utils/fixtures';
45

56
// pool.query 모킹
67
jest.mock('@/configs/db.config', () => ({
@@ -184,14 +185,6 @@ describe('인증 미들웨어', () => {
184185
refreshToken: 'refresh-token'
185186
};
186187

187-
// 사용자 정보 mock
188-
const mockUser = {
189-
id: 1,
190-
username: 'testuser',
191-
192-
velog_uuid: 'c7507240-093b-11ea-9aae-a58a86bb0520'
193-
};
194-
195188
// DB 쿼리 결과 모킹
196189
(pool.query as jest.Mock).mockResolvedValueOnce({
197190
rows: [mockUser]

src/repositories/__test__/fixtures.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/repositories/__test__/leaderboard.repo.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Pool } from 'pg';
22
import { DBError } from '@/exception';
3-
import { LeaderboardRepository } from '@/repositories/leaderboard.repository';
43
import { UserLeaderboardSortType, PostLeaderboardSortType } from '@/types';
5-
import { mockPool, createMockQueryResult } from './fixtures';
4+
import { LeaderboardRepository } from '@/repositories/leaderboard.repository';
5+
import { mockPool, createMockQueryResult } from '@/utils/fixtures';
66

77
jest.mock('pg');
88

src/repositories/__test__/post.repo.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Pool } from 'pg';
22
import { PostRepository } from '@/repositories/post.repository';
33
import { DBError } from '@/exception';
4-
import { mockPool, createMockQueryResult } from './fixtures';
4+
import { mockPool, createMockQueryResult } from '@/utils/fixtures';
55

66
jest.mock('pg');
77

src/repositories/__test__/qr.repo.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { UserRepository } from '@/repositories/user.repository';
22
import { DBError } from '@/exception';
33
import { Pool } from 'pg';
44
import { QRLoginToken } from "@/types/models/QRLoginToken.type";
5-
import { mockPool } from './fixtures';
5+
import { mockPool } from '@/utils/fixtures';
66

77
jest.mock('pg');
88

src/repositories/__test__/totalStats.repo.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Pool } from 'pg';
2-
import { TotalStatsRepository } from '@/repositories/totalStats.repository';
32
import { DBError } from '@/exception';
4-
import { getKSTDateStringWithOffset } from '@/utils/date.util';
5-
import { mockPool, createMockQueryResult } from './fixtures';
63
import { TotalStatsType } from '@/types';
4+
import { TotalStatsRepository } from '@/repositories/totalStats.repository';
5+
import { getKSTDateStringWithOffset } from '@/utils/date.util';
6+
import { mockPool, createMockQueryResult } from '@/utils/fixtures';
77

88
// Mock dependencies
99
jest.mock('@/configs/logger.config', () => ({

src/services/__test__/qr.service.test.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { Pool } from 'pg';
2+
import { DBError } from '@/exception';
13
import { UserService } from '@/services/user.service';
24
import { UserRepository } from '@/repositories/user.repository';
3-
import { DBError } from '@/exception';
45
import { QRLoginToken } from '@/types/models/QRLoginToken.type';
5-
import { User } from '@/types';
6-
import { Pool } from 'pg';
6+
import { mockUser } from '@/utils/fixtures';
77

88
// AESEncryption 클래스 모킹
99
jest.mock('@/modules/token_encryption/aes_encryption', () => {
@@ -68,18 +68,6 @@ describe('UserService의 QR 로그인 기능', () => {
6868
});
6969

7070
describe('useToken', () => {
71-
const mockUser: User = {
72-
id: 1,
73-
velog_uuid: 'uuid-1',
74-
access_token: 'encrypted-access-token',
75-
refresh_token: 'encrypted-refresh-token',
76-
77-
group_id: 1,
78-
is_active: true,
79-
created_at: new Date(),
80-
updated_at: new Date()
81-
};
82-
8371
const mockQRToken: QRLoginToken = {
8472
id: 1,
8573
token: 'token',

src/utils/fixtures.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { QueryResult } from 'pg';
2+
import { User } from '@/types';
3+
4+
/**
5+
* PostgreSQL 쿼리를 모킹하기 위한 mock Pool 객체
6+
*
7+
* @description Jest 테스트에서 pg.Pool의 query 메서드를 모킹하는 데 사용됩니다.
8+
* @example
9+
* ```typescript
10+
* // 성공적인 쿼리 결과 모킹
11+
* mockPool.query.mockResolvedValue(createMockQueryResult([{ id: 1, name: 'test' }]));
12+
*
13+
* // 에러 발생 모킹
14+
* mockPool.query.mockRejectedValue(new Error('Database error'));
15+
* ```
16+
*/
17+
export const mockPool: {
18+
query: jest.Mock<Promise<QueryResult<Record<string, unknown>>>, unknown[]>;
19+
} = {
20+
query: jest.fn(),
21+
};
22+
23+
/**
24+
* 테스트용 모의 사용자 데이터, User 객체
25+
*
26+
* @description 인증 관련 (미들웨어, QR 로그인 등) 유닛 테스트에서 공통적으로 사용되는 mock User 객체입니다.
27+
*/
28+
export const mockUser: User = {
29+
id: 1,
30+
velog_uuid: 'uuid-1',
31+
access_token: 'encrypted-access-token',
32+
refresh_token: 'encrypted-refresh-token',
33+
34+
username: 'nuung',
35+
thumbnail: 'https://nuung.com/test.jpg',
36+
group_id: 1,
37+
is_active: true,
38+
created_at: new Date('2024-01-01T00:00:00Z'),
39+
updated_at: new Date('2024-01-01T00:00:00Z'),
40+
};
41+
42+
/**
43+
* pg의 QueryResult 타입을 만족하는 mock 객체를 생성하기 위한 헬퍼 함수
44+
*
45+
* @template T - 쿼리 결과 row의 타입 (Record<string, unknown>를 확장해야 함)
46+
* @param rows - 모킹할 데이터베이스 행들의 배열
47+
* @returns PostgreSQL QueryResult 형태의 mock 객체
48+
*
49+
* @description
50+
* PostgreSQL의 실제 쿼리 결과와 동일한 구조를 가진 mock 객체를 생성합니다.
51+
* Jest 테스트에서 데이터베이스 쿼리 결과를 모킹할 때 사용됩니다.
52+
*
53+
* @example
54+
* ```typescript
55+
* // 사용자 데이터 모킹
56+
* const mockUsers = [
57+
* { id: 1, name: 'John', email: 'john@example.com' },
58+
* { id: 2, name: 'Jane', email: 'jane@example.com' }
59+
* ];
60+
* const result = createMockQueryResult(mockUsers);
61+
*
62+
* // 빈 결과 모킹
63+
* const emptyResult = createMockQueryResult([]);
64+
*
65+
* // Jest mock에서 사용
66+
* mockPool.query.mockResolvedValue(createMockQueryResult(mockUsers));
67+
* ```
68+
*/
69+
export function createMockQueryResult<T extends Record<string, unknown>>(rows: T[]): QueryResult<T> {
70+
return {
71+
rows,
72+
rowCount: rows.length,
73+
command: '',
74+
oid: 0,
75+
fields: [],
76+
} satisfies QueryResult<T>;
77+
}

0 commit comments

Comments
 (0)