웹 풀사이클 데브코스 TIL [Day 83] - 단위 테스트와 컨테이너화

JaeKyung Hwang·2024년 3월 26일
0
post-thumbnail

2024.03.25(월)

🧪단위 테스트

💾Backend

  • 테스트 라이브러리 (devDependencies)
    • jest (Method Level)
      • manual mock을 테스트 파일에서 사용하려면 jest.mock('./moduleName')로 명시적으로 호출해주어야 함 (mapping도 가능)
        • manual mock filed은 보통 mocking하려는 파일과 인접한 __mocks__ 폴더에 작성
      • node module을 mock하려면 node_modules와 인접한 __mocks__ 폴더에 동일한 module 명으로 mock 모듈 파일을 작성하면 됨
        • 테스트 시 자동으로 해당 모듈을 대치해 mock 모듈이 사용됨
        • 즉, 명시적으로 jest.mock('./moduleName')을 호출할 필요 ❌
      .
      ├── config
      ├── __mocks__
      │   └── fs.js
      ├── models
      │   ├── __mocks__
      │   │   └── user.js
      │   └── user.js
      ├── node_modules
      └── views
    • supertest (API Level)
      • supertest의 request 모듈로 테스트에 필요한 HTTP 요청을 생성하는데 사용
  • 설치
    npm i -D jest ts-jest @types/jest supertest @types/supertest
  • Jest가 TypeScript를 ts-jest로 변환하려면 config 파일을 생성해야 하며 다음 명령어로 자동으로 생성 가능
    npx ts-jest config:init
    • jest.config.js
      /** @type {import('ts-jest').JestConfigWithTsJest} */
      module.exports = {
        preset: 'ts-jest',
        testEnvironment: 'node',
      };
  • tsconfig.json
    	…,
        "exclude": [
            "src/**/*.test.ts",
            "src/**/__mocks__/*.ts"
        ]
    }
  • 테스트 명령어
    npm test
  • Makefile (반드시 탭(Tab) 사용)
    all:
    	cat ./Makefile
    
    test:
    	npm test
    • make all: 현재 Makefile의 내용을 출력
    • make test: 프로젝트의 테스트를 실행 (전제조건: 프로젝트가 Node.js 및 npm을 사용하여 관리되고 있어야 함)

🖥️Frontend

  • 테스트 라이브러리 (이미 설치되어 있음)
    • @testing-library/jest-dom
      • package.json에 jest에 의한 모듈 이름 매핑 설정
        "jest": {
        	"moduleNameMapper": {
        		"^@/(.+)": "<rootDir>/src/$1"
        	}
        }
    • @testing-library/react
    • @testing-library/user-event
  • 테스트 명령어
    npm test
    CI=true npm test
  • Makefile (반드시 탭(Tab) 사용)
    all:
    	cat ./Makefile
    
    test:
    	CI=true npm test
    • make all: 현재 Makefile의 내용을 출력
    • make test: 프로젝트의 테스트를 CI 모드로 실행

📦컨테이너화




profile
이것저것 관심 많은 개발자👩‍💻

0개의 댓글