TDD강의를 들으면서 Github Actions로 jest를 사용하는 CI 파이프라인 구축하던 중 에러가 발생하여 정리해보고자 작성한다.
'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `-detectOpenHandles` to troubleshoot this issue.
Error: Process completed with exit code 1.
로그가 무수히 길게 나오는데 슥슥 올라가다 보면 이런 문구가 있다.
PASS test/unit/products.test.js
FAIL test/integration/products.int.test.js (20.566 s)
● Console
console.log
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
결국 mongoDB에 연결하지 못한다는 이야기인데, 기존에 로컬테스트 진행시 잘되었는데 왜 안되지 ? 하며 로컬에서 서버를 열거나 테스트 진행시 똑같은 문구를 발견하게되어 env파일을 열어보았는데 기존에 환경변수에 따옴표를 넣었는데 제거하고 테스트 해보니 정상적으로 진행되었다.
// 변경 전
PORT=5000;
MONGO_DB_URL='mongodb+srv://{user}:{password}@cluster0.hg1pk.mongodb.net/{dbname}?retryWrites=true&w=majority'
// 변경 후
PORT=5000;
MONGO_DB_URL='mongodb+srv://{user}:{password}@cluster0.hg1pk.mongodb.net/{dbname}?retryWrites=true&w=majority'
GitHub Actions Test 진행 중 종료되지 않는 현상
test가 완료되었지만 npx jest 명령어를 이용하여 실행되었고 테스트가 끝난 후 서버가 정상적으로 닫히지 않아 발생한 문제로 추정된다.
Jest를 이용한 TDD 코드 작성 중 에러
1. 문제 상황 TDD 강의를 들으면서 jest를 이용하여 통합테스트 코드를 작성 후 진행하니 아래와 같은 메세지가 나왔다. A worker process has failed to exit gracefully and has been force exited. This is likely caused by t
jang-kroed.tistory.com
결국 우려했던 사태가 터진거같다..
우선적으로 --runInBand --detectOpenHandles 옵션을 추가하여 실행하는 방법을 적용해보았다.
Error: The operation was canceled.
테스트는 모두 통과했으나 시간초과때문인지 작업이 취소되었다.
로컬에서 테스트 해보면서 해결방법을 찾아보았는데, 서버를 닫아주기 보다 db상태가 connect상태로 테스트가 종료되어서 생긴 문제같았다.
db연결을 disconnect 해준 뒤 테스트 진행하니 경고문구가 사라져 다시 진행해보았다.
const mongoose = require('mongoose');
afterAll(() => {
mongoose.disconnect();
});
성공 !