문제 상황
jest가 import된 모듈을 읽을 때 절대경로를 제대로 읽지 못하는 오류로 추측했다.
//package.json
// 변경 전
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
module의 절대경로는 vsc의 자동설정을 따랐고, 자연스레 src부터 시작했다.
추측하기로는 해당 파일이 있는 directory에서부터 module의 경로를 들어가다보니 module을 읽지 못한 것이 아닐까 싶다.
해결방안
도움이 된 해결책은 다름 아닌 유튜버 https://www.youtube.com/watch?v=bMrclYWNIE8
(보통 구글링 할 때 유튜버는 괜히 신뢰가 가질 않아 들어가질 않았는데, 생각이 바뀌었다.)
요지는, jest config에서 module 절대경로를 읽어들어갈 때의 경로를 modulePaths 옵션을 활용하여 규정하자는 것이다.
따라서 modulePaths를 통해 module의 경로 시작 지점을 root로 설정하고, root는 지금 있는 directory보다 상위에서 시작하게끔 config를 수정하였다.
// package.json
// 변경 후
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "./",
"modulePaths": ["<rootDir>"],
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
'Web' 카테고리의 다른 글
[node.js] node-schedule, schedulejob 객체(readonly) 수정하기 (0) | 2022.08.30 |
---|---|
[Nest] Internal server error (0) | 2022.08.25 |
[Express] body parser? urlencoded? (0) | 2022.08.24 |
[CORS] CSR에서 google oauth2.0 적용기 (0) | 2022.07.29 |
[Express] request 객체에 object 담기 (0) | 2022.07.01 |