1. 문제 상황
- TDD 강의를 들으면서 jest를 이용하여 통합테스트 코드를 작성 후 진행하니 아래와 같은 메세지가 나왔다.
A worker process has failed to exit gracefully and has been force exited.
This is likely caused by tests leaking due to improper teardown.
Try running with --detectOpenHandles to find leaks.
Active timers can also cause this,
ensure that .unref() was called on them. Answer in English.
"jest --detectOpenHandles" 명령어를 사용하여 원인이 되는 코드를 불러올 수 있다고 한다.
Test Suites: 2 passed, 2 total
Tests: 20 passed, 20 total
Snapshots: 0 total
Time: 5.665 s
Ran all test suites.
Jest has detected the following 1 open handle potentially keeping Jest from exiting:
● TCPSERVERWRAP
29 | });
30 |
> 31 | app.listen(PORT);
| ^
32 | console.log(`Runnig on port ${PORT}`);
33 |
34 | module.exports = app;
at Function.listen (node_modules/express/lib/application.js:635:24)
at Object.listen (server.js:31:5)
at Object.require (test/integration/products.int.test.js:2:13)
원인으로는 매 테스트코드 마다 서버와 DB를 새로 불러오는 코드로 작성되어 있어 경고문구가 출력되는 것.
2. 해결 방법
- jest cli 문서 참고하다보니, --runInBand라는 jest 실행 옵션이 해결해줄 수 있는 것으로 보였다.
- 해당 옵션의 역할은 다음과 같은 내용이다.
Alias: -i. Run all tests serially in the current process, rather than creating a worker pool of child processes that run tests. This can be useful for debugging. - 테스트 실행 시 새로운 프로세스를 만들지 않고 처음 만들어진 프로세스 풀에서 계속 테스트를 실행한다는 것으로 보인다.
// package.json
{
"name": "tdd-app",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "jest --runInBand --detectOpenHandles",
"start": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2",
"mongoose": "^7.0.2"
},
"devDependencies": {
"jest": "^29.5.0",
"node-mocks-http": "^1.12.2",
"nodemon": "^2.0.21",
"supertest": "^6.3.3"
}
}
3. 결과
jest scripts 수정 후 테스트코드 동작시켜보니 기존에 npm start 하고나서 서버를 끌때 ctrl + c 단축키를 눌러 서버를 닫듯이, 테스트 코드가 모두 작동하고 그대로 끝나지 않고 따로 종료시켜주는 방식인것같다.
추후 CI과정에서 오류가 나지 않을까 생각되어 afterAll 함수를 활용한 방식을 사용해야 할 것 같다.
4. 참고자료
Jest CLI Options · Jest
The jest command line runner has a number of useful options. You can run jest --help to view all available options. Many of the options shown below can also be used together to run tests exactly the way you want. Every one of Jest's Configuration options c
jestjs.io
error - 인프런 | 질문 & 답변
안녕하세요 :) 이 error가 나는 경우는 어떻게 하면 되나요? A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leak...
www.inflearn.com