# package.json
{
...,
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
Cannot find module 'src/collections/entities/collection.entity' from 'auth/entities/user.entity.ts'
Require stack:
auth/entities/user.entity.ts
auth/auth.service.ts
auth/auth.controller.ts
auth/auth.controller.spec.ts
> 1 | import { Collection } from 'src/collections/entities/collection.entity'; | ^
2 | import { Photospot } from 'src/collections/entities/photospot.entity';
3 | import { Join } from 'src/meetups/entities/join.entity';
4 | import { Meetup } from 'src/meetups/entities/meetup.entity';
at Resolver._throwModNotFoundError (../node_modules/jest-resolve/build/resolver.js:427:11)
at Object.<anonymous> (auth/entities/user.entity.ts:1:1)
at Object.<anonymous> (auth/auth.service.ts:4:1)
at Object.<anonymous> (auth/auth.controller.ts:2:1)
at Object.<anonymous> (auth/auth.controller.spec.ts:2:1)
테스트 환경으로 코드를 실행할 때에 src 경로로 쓰여진 절대 경로를 해석하질 못 하기 때문이다. jest config를 수정해줘야한다.
# package.json
"jest": {
...,
"moduleNameMapper": {
"src/(.*)": "<rootDir>/$1"
}
}
src로 시작하는 절대 경로를<rootDir>를 이용하여 매핑해주는 설정을 넣어주면 된다.