๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
NestJS

[error] Jest did not exit one second after the test run has completed. ํ•ด๊ฒฐ

by bba_dda 2021. 12. 21.
๋ฐ˜์‘ํ˜•

์ €๋ฒˆ์— ์ง„ํ–‰ํ•œ ์›ํ‹ฐ๋“œ ๋ฐฑ์—”๋“œ ํ”„๋ฆฌ์˜จ๋ณด๋”ฉ์ฝ”์Šค 4๋ฒˆ์งธ ๊ณผ์ œ์˜ ํ…Œ์ŠคํŠธ์ฝ”๋“œ ์ž‘์„ฑ ์ค‘ ๋ฐœ๊ฒฌํ•œ ์—๋Ÿฌ์ด๋‹ค.

 

e2e test๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ, test์ข…๋ฃŒ ํ›„์— ์•„๋ž˜์™€ ๊ฐ™์€ ๋…ธ๋ž€ ๊ฒฝ๊ณ  ๋ฉ”์„ธ์ง€๊ฐ€ ๋“ฑ์žฅํ–ˆ๋‹ค!

๋„ˆ๋Š” ๋ˆ„๊ตฌ๋ƒ!

๋ฉ”์„ธ์ง€๋ฅผ ๊ทธ๋Œ€๋กœ ํ•ด์„ํ•ด๋ณด๋ฉด 

Jest did not exit one second after the test run has completed.

→ Jest๊ฐ€ test๊ฐ€ ์™„๋ฃŒ๋˜๊ณ  1์ดˆ ํ›„์—๋„ exit๋˜์ง€ ์•Š์•˜๋‹ค. 

→ Jest๊ฐ€ ์•ˆ๋๋‚œ๋‹ค!! (๋ญ”๊ฐ€ ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ณ  ๋‚จ์•„์žˆ๋‹ค)

 

This usally means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with '--detectOpenHandles' to troubleshoot this issue.

→ test์•ˆ์— ์ข…๋ฃŒ๋˜์ง€ ์•Š์€ ๋น„๋™๊ธฐ ์ž‘์—…์ด ์žˆ์„ ๋•Œ ๋ฐœ์ƒํ•˜๋”๋ผ. --detectOpenHandles ์˜ต์…˜ ์‚ฌ์šฉ์„ ๊ณ ๋ คํ•ด๋ด๋ผ. 

 

๊ทธ๋ž˜์„œ '์นœ์ ˆํ•˜๊ฒŒ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•๋„ ์•Œ๋ ค์ฃผ๋Š”๊ตฌ๋‚˜' ํ•˜๊ณ  --detectOpenHandles ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค. 

 

๊ทธ๋Ÿฌ๋‚˜ JEST --detectOpenHandles ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณด๋‹ˆ

Attempt to collect and print open handles preventing Jest from exiting cleanly. Use this in cases where you need to use --forceExit in order for Jest to exit to potentially track down the reason. This implies --runInBand, making tests run serially. Implemented using async_hooks. This option has a significant performance penalty and should only be used for debugging.

๋‚ด๊ฐ€ ์ดํ•ดํ•œ ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
์‹คํ–‰์ค‘์ธ๊ฒŒ ์žˆ์œผ๋ฉด Jest๊ฐ€ ์™„๋ฒฝํžˆ ์ข…๋ฃŒ๋˜์ง€ ์•Š์œผ๋‹ˆ, ๊ฐ•์ œ์ข…๋ฃŒ๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์ด ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด๋ผ.
๊ทธ๋ ‡์ง€๋งŒ ์ด ์˜ต์…˜์€ ์„ฑ๋Šฅ์ด ๋งŽ์ด ์ €ํ•˜๋˜๋‹ˆ ๋””๋ฒ„๊น…์—๋งŒ ์‚ฌ์šฉํ•ด๋ผ. 

๊ทธ๋ž˜์„œ ์ด ์˜ต์…˜ ๋ง๊ณ  ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ดค๋‹ค.

 

์ข…๋ฃŒ๋˜์ง€ ์•Š์€ ๋น„๋™๊ธฐ ์ž‘์—…์ด ๋ฌด์—‡์ธ์ง€ ์ฐพ์•„์„œ, ๊ฑ”๋ฅผ ์ข…๋ฃŒํ•ด์ฃผ๋ฉด ๋  ๊ฒƒ์ด๋‹ค!

 

์ปค๋ฎค๋‹ˆํ‹ฐ๋ฅผ ๋’ค์ ธ๋ณด๋‹ˆ test ์ข…๋ฃŒํ›„์— DB connection์„ closeํ•ด๋ณด๋ผ๋Š” ์˜๊ฒฌ์ด ๊ต‰์žฅํžˆ ๋งŽ์•˜๋Š”๋ฐ,

์šฐ๋ฆฌ ์ฝ”๋“œ์—์„œ๋Š” ๋ช…์‹œ์ ์œผ๋กœ connection์„ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„์„œ, ์–ด๋–ป๊ฒŒ close๋ฅผ ํ•˜๋ผ๋Š”๊ฑด์ง€ ํ•œ์ฐธ ํ—ค๋งธ๋‹ค.

 

๋‹ต์€ TypeormModule.forRoot() ์•ˆ์— ์˜ต์…˜์œผ๋กœ ์„ค์ •ํ•œ keepConnectionAlive ๋ฅผ false๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

keepConnectionAlive: true ์˜ต์…˜์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ ํ›„์—๋„ ์—ฐ๊ฒฐ์ด ์ข…๋ฃŒ๋˜์ง€ ์•Š๋„๋ก ํ•ด์ค€๋‹ค.

๊ทธ๋ž˜์„œ ๋ฌธ์ œ๊ฐ€ ๋˜์—ˆ๋˜ ๊ฒƒ์ด๋‹ค!

 

๊ทธ๋ ‡์ง€๋งŒ keepConnectionAlive: false๋กœ ๋ฐ”๊พธ์ž ๋‹ค๋ฅธ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

๋ ์šฉ "defalut" ๋ผ๋Š” ์ด๋ฆ„์˜ ์ปค๋„ฅ์…˜์„ ์ค‘๋ณต์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค!

 ์ด ์—๋Ÿฌ๋ฅผ ๋งˆ์ฃผํ•˜์ž ๋ฌธ๋“ ํ”„๋กœ์ ํŠธ ์ˆ˜ํ–‰ ๋‹น์‹œ์˜ ๊ธฐ์–ต์ด ๋– ์˜ฌ๋ž๋‹ค.

keepConnectionAlive ์˜ต์…˜์€ ๊ธฐ๋ณธ ๊ฐ’์ด false์ด๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ”„๋กœ์ ํŠธ ์ง„ํ–‰์ค‘์— ์œ„์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ฒจ์„œ, ์ € ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด keepConnectionAlive: true์„ ์ถ”๊ฐ€ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด, ์ด ์ค‘๋ณต ์ปค๋„ฅ์…˜ ์—๋Ÿฌ๋Š” ์™œ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ผ๊นŒ? 

 

๋‹ต์€ e2e test ์ฝ”๋“œ์— ์žˆ์—ˆ๋‹ค.

app.init์„ beforeEach ์ฆ‰, ๋งค test๋งˆ๋‹ค ํ•˜๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๋งค test๋งˆ๋‹ค init์„ ํ•˜๋‹ˆ, default ์ปค๋„ฅ์…˜์ด ์ค‘๋ณต๋˜์–ด ๋ฌธ์ œ๊ฐ€ ๋˜์—ˆ๋˜ ๊ฒƒ์ด๋‹ค.

beforeEach๋ฅผ beforeAll๋กœ ๋ฐ”๊พธ์–ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

beforeEach(async () => {
    const moduleFixture: TestingModule = await Test.createTestingModule({
        imports: [AppModule]
    }).compile();

    app = moduleFixture.createNestApplication();
    await app.init();
});

 

์†Œ๊ฐ ์—ด ๋งˆ๋”” 

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ๋‹น์‹œ์—, default connection ์ค‘๋ณต ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ

๋ฌด์ž‘์ • keepConnectionAlive: true์„ ์ถ”๊ฐ€ํ•˜๊ธฐ๋ณด๋‹ค

์™œ ์ €๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„๊นŒ๋ฅผ ๋จผ์ € ์ดํ•ดํ–ˆ์œผ๋ฉด ์ข‹์ง€ ์•Š์•˜์„๊นŒ ํ•˜๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

๊ฒ€์ƒ‰์œผ๋กœ ์›ฌ๋งŒํ•œ error์˜ ํ•ด๊ฒฐ๋ฐฉ์‹์„ ์ฐพ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋ ‡๊ฒŒ ๊ฒ€์ƒ‰์œผ๋กœ error๋ฅผ ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€๋Š”๊ฒŒ ์ข‹์€ ํƒœ๋„์ธ๊ฐ€๋Š” ์ƒ๊ฐํ•ด๋ณผ ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

๊ฒ€์ƒ‰์ด ๋‚˜์˜๋‹ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค.

๋ฌด์ž‘์ • ํ•ด๊ฒฐํ•˜๋ ค ํ•˜๊ธฐ๋ณด๋‹จ ๋จผ์ € ์™œ ์ด๋Ÿฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€๋ฅผ ์ถฉ๋ถ„ํžˆ ์ดํ•ดํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

ํ•œํŽธ์œผ๋กœ๋Š”, ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ๋‹น์‹œ๋ณด๋‹ค Nest ๋ฐ test์ฝ”๋“œ ์ž‘์„ฑ์— ๋Šฅ์ˆ™ํ•ด์ ธ์„œ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์•˜๋‚˜ํ•˜๋Š” ์ƒ๊ฐ๋„ ๋“ ๋‹ค.

์‹œ๊ฐ„์— ์ซ“๊ธฐ์ง€ ์•Š์œผ๋‹ˆ ๋Š๊ธ‹ํ•˜๊ฒŒ error๋ฅผ ํƒ๊ตฌํ•  ์ˆ˜ ์žˆ์–ด ์ข‹๋‹ค. 

 

e2e test๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๋ฐœ๊ฒฌํ•œ error๊ฐ€ ํ•˜๋‚˜ ๋” ์žˆ๋Š”๋ฐ, ๊ทธ๊ฒƒ๋„ ์–ด์„œ ํ•ด๊ฒฐํ•ด๋ณด๊ณ  ์‹ถ๋‹ค!

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€