인프런 커뮤니티 질문&답변
Cannot log after tests are done. Did you forget to wait for something async in your test?
작성
·
2.1K
답변 1
1
John Ahn
지식공유자
안녕하세요 !!!
좋은 질문 감사합니다 !
세가지 정도의 방법이 있는것 같은데요...
1.
"test": "jest --runInBand --detectOpenHandles"
우선 이 스크립트를 이용해서 npm run test 로 하면 저 에러가 안나옵니다!
2.
그리고 현재 jest testrun을 이용해서 하나씩 할 때는
node '/Users/johnahn/Downloads/tdd-app/node_modules/.bin/jest' '/Users/johnahn/Downloads/tdd-app/test/integration/products.int.test.js' -t 'POST /api/products'
이런식의 명령어를 사용하는데 거기에 --forceExit 를 붙여주면 에러가 안납니다.
3.
테스트 후에 몽고db를 강제로 close 해주시면 되는데 이렇게 되면 여러개를 한꺼번에 테스트할 때는 오히려 에러가 납니다. 왜냐면 하나의 테스트 후에 강제로 디비를 종료하기 때문입니다.
const mongoose = require('mongoose');
let firstProduct;
it("POST /api/products", async (done) => {
const response = await request(app)
.post("/api/products")
.send(newProduct);
expect(response.statusCode).toBe(201)
expect(response.body.name).toBe(newProduct.name)
expect(response.body.description).toBe(newProduct.description)
await mongoose.connection.close();
done();
})
그래서 integration 테스트 같은 경우는 npm run test로 테스트해주시는게 제일 좋은것같습니다 감사합니다




