Skip to content

Conversation

@hyoseonlim
Copy link
Member

@hyoseonlim hyoseonlim commented Apr 12, 2025

🛠️ 코드 리팩토링

기존의 상대경로 import를 alias 설정을 통해 절대경로 방식으로 개선하였습니다.

👎🏼 Before
image

👍🏼 After
image

변경된 환경

  1. tsconfig.json에서 src 폴더 내 패키지들에 alias를 아래와 같이 설정했습니다.
"baseUrl": "./src",
"paths": {
  "@auth/*": ["auth/*"],
  "@business/*": ["business/*"],
  "@chat/*": ["chat/*"],
  "@common/*": ["common/*"],
  "@config/*": ["config/*"],
  "@customer/*": ["customer/*"],
  "@driver/*": ["driver/*"],
  "@email/*": ["email/*"],
  "@env/*": ["env/*"],
  "@pet/*": ["pet/*"],
  "@pre-registration-servey/*": ["pre-registration-servey/*"],
  "@schemas/*": ["schemas/*"],
  "@system/*": ["system/*"],
  "@terms/*": ["terms/*"],
  "@mock/*": ["../test/mock/*"]
}
  1. jest.config.ts 파일을 만들어 Jest가 테스트를 실행할 때 tsconfig.json의 경로 설정을 인식하게끔 연결했습니다. 이때 tsconfig.json 파일에 아래 설정값을 추가해서 JSON 파일을 import할 수 있도록 처리했습니다.
"resolveJsonModule": true,
"esModuleInterop": true

참고 사항

nest build 시에 src 외부의 test 폴더는 포함되지 않기 때문에 "@mock/": ["../test/mock/"] 이 값이 에러를 발생시킬 수 있다고 합니다.
@ mock는 테스트 파일에서만 사용되므로 tsconfig.json에서 제거 후 jest.config.ts 에서 아래처럼 수동 매핑 처리를 해줬는데, 적용이 안됩니다. ㅜㅜ

moduleNameMapper: {
  ...pathsToModuleNameMapper(compilerOptions.paths, {
    prefix: '<rootDir>/src/',
  }),
  '^@mock/(.*)$': '<rootDir>/test/mock/$1',
}

그래서 tsconfig.test.ts 파일을 새로 만들어 해당 파일에서 설정해도 적용이 안됩니다..
test 폴더도 src 내부로 옮기면 해결되겠지만 빌드 대상에 테스트 폴더를 포함시키는 게 좋지 않은 것 같아서요
관련해서 의견 부탁드립니다 😿

@hyoseonlim hyoseonlim marked this pull request as ready for review April 13, 2025 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants