Skip to content

유닛 테스트 추가 #2

@DaleSeo

Description

@DaleSeo

현재 상황

프로젝트에 유닛 테스트가 전혀 없습니다.

  • 테스트 프레임워크 미설정
  • 핵심 유틸리티 함수들의 동작 검증 불가

목표

utils/ 폴더의 핵심 유틸리티 함수에 대한 유닛 테스트 추가

테스트 파일은 애플리케이션 코드 옆에 배치 (예: github.jsgithub.test.js)

테스트 대상

utils/github.js

  • createJWT() - JWT 생성 및 서명
  • importPrivateKey() - PKCS8 Private Key 파싱
  • sign() - RS256 서명 생성
  • base64UrlEncode() - Base64 URL 인코딩

utils/cors.js

  • corsResponse() - CORS 헤더 포함 응답
  • errorResponse() - 에러 응답 포맷

파일 구조

utils/
├── github.js
├── github.test.js          # 새로 추가
├── cors.js
├── cors.test.js            # 새로 추가
├── webhook.js
└── ... (기타 파일들)

구현 가이드

1. 테스트 프레임워크 설치

npm install -D vitest

2. 설정 파일 생성

vitest.config.js:

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,
  },
});

3. package.json에 스크립트 추가

{
  "scripts": {
    "test": "vitest run",
    "test:watch": "vitest"
  }
}

4. 테스트 파일 작성 예시

utils/github.test.js:

import { describe, it, expect } from 'vitest';
import { base64UrlEncode } from './github.js';

describe('base64UrlEncode', () => {
  it('should encode Uint8Array to base64url', () => {
    const input = new Uint8Array([72, 101, 108, 108, 111]);
    const result = base64UrlEncode(input);
    
    expect(result).toBe('SGVsbG8');
  });
});

utils/cors.test.js:

import { describe, it, expect } from 'vitest';
import { corsResponse, errorResponse } from './cors.js';

describe('corsResponse', () => {
  it('should include CORS headers', () => {
    const response = corsResponse({ success: true });
    
    expect(response.headers.get('Access-Control-Allow-Origin')).toBe('*');
  });
});

describe('errorResponse', () => {
  it('should return error with status code', () => {
    const response = errorResponse('Not found', 404);
    
    expect(response.status).toBe(404);
  });
});

체크리스트

  • Vitest 설치
  • vitest.config.js 생성
  • package.json에 test 스크립트 추가
  • utils/github.test.js 작성
  • utils/cors.test.js 작성
  • npm test로 모든 테스트 통과 확인

참고

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions