Skip to content

detectAsyncLeaks

  • 类型: boolean
  • CLI: --detectAsyncLeaks, --detect-async-leaks
  • 默认值: false

WARNING

启用此选项会使测试运行速度变慢很多。仅在调试或开发测试时使用。

检测从测试文件泄漏的异步资源。 使用 node:async_hooks 来跟踪异步资源的创建。如果资源未被清理,它将在测试结束后被记录。

例如,如果你的代码中有 setTimeout 调用在测试结束后执行回调,你将看到以下错误:

sh
⎯⎯⎯⎯⎯⎯⎯⎯ Async Leaks 1 ⎯⎯⎯⎯⎯⎯⎯⎯

Timeout leaking in test/checkout-screen.test.tsx
 26|
 27|   useEffect(() => {
 28|     setTimeout(() => setWindowWidth(window.innerWidth), 150)
   |     ^
 29|   })
 30|

要修复此问题,你需要确保代码正确地清理了 timeout:

js
useEffect(() => {
  setTimeout(() => setWindowWidth(window.innerWidth), 150) 
  const timeout = setTimeout(() => setWindowWidth(window.innerWidth), 150) 

  return function cleanup() { 
    clearTimeout(timeout) 
  } 
})