我的Jests测试发生内存泄漏,如何修复?

tgabmvqs  于 2022-12-08  发布在  Jest
关注(0)|答案(4)|浏览(311)

当我运行jest测试时,每次测试所使用的内存量会随着时间的推移而增加。相反,我在CircleCI上运行测试时发现了这一点。2我得到了以下错误:

FAIL  __tests__/pages/login.test.tsx
 
  ● Test suite failed to run
 
    jest: failed to cache transform results in: /tmp/jest_2ne/jest-transform-cache-7bdebd1a0c578519274d14a78b89f87c-f8238a99880aac6151736010e575fab1/0b/symbols_0bf4cffb45cb261625f2f3fca21a4789.map
 
    Failure message: ENOMEM: not enough memory, write
 
      at writeFileSync (node_modules/write-file-atomic/index.js:215:10)
      at writeCacheFile (node_modules/@jest/transform/build/ScriptTransformer.js:809:33)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:554:7)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:586:40)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:624:25)

我如何修复我的jest配置以防止这种情况?

egmofgnx

egmofgnx1#

首先,通过使用以下选项运行jest,确保您的测试有漏洞:jest --logHeapUsage
运行测试并检查内存消耗是否随时间增加,如下所示(文件名已更改):

PASS  __tests__/pages/file.test.tsx (181 MB heap size)
 PASS  __tests__/pages/file2.test.tsx (193 MB heap size)
 PASS  __tests__/components/Header/file3.test.tsx (201 MB heap size)
 PASS  __tests__/components/Header/file4.test.tsx (192 MB heap size)
 PASS  __tests__/components/Header/file5.test.tsx (218 MB heap size)
 PASS  __tests__/pages/file6.test.tsx (201 MB heap size)
 PASS  __tests__/components/file6.test.tsx (203 MB heap size)
 PASS  __tests__/components/file7.test.tsx (213 MB heap size)
 PASS  __tests__/components/file8.test.tsx (234 MB heap size)
 PASS  __tests__/components/file9.test.tsx (222 MB heap size)
 PASS  __tests__/components/file10.test.tsx (240 MB heap size)
 PASS  __tests__/components/file11.test.tsx (231 MB heap size)
 PASS  __tests__/utils/file12.test.tsx (239 MB heap size)
 PASS  __tests__/components/file13.test.tsx (251 MB heap size)
 PASS  __tests__/components/file14.test.tsx (239 MB heap size)
 PASS  __tests__/components/file15.test.tsx (249 MB heap size)
 PASS  __tests__/components/file16.test.tsx (143 MB heap size)

要解决此问题,请将package.json中的npm run testyarn test命令更改为:node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage
运行该命令,输出如下:

PASS  __tests__/components/file.test.tsx (143 MB heap size)
 PASS  __tests__/pages/onboarding/file1.test.tsx (149 MB heap size)
 PASS  __tests__/components/file2.test.tsx (146 MB heap size)
 PASS  __tests__/components/file3.test.tsx (146 MB heap size)
 PASS  __tests__/components/file4.test.tsx (153 MB heap size)
 PASS  __tests__/components/Header /file5.test.tsx (149 MB heap size)
 PASS  __tests__/pages/file6.test.tsx (149 MB heap size)
 PASS  __tests__/pages/file7.test.tsx (149 MB heap size)
 PASS  __tests__/components/file8.test.tsx (147 MB heap size)
 PASS  __tests__/components/file9.test.tsx (148 MB heap size)
 PASS  __tests__/pages/file10.test.tsx (148 MB heap size)
 PASS  __tests__/components/Header /file11.test.tsx (148 MB heap size)
 PASS  __tests__/functions/file12.test.tsx (149 MB heap size)
 PASS  __tests__/components/file13.test.tsx (148 MB heap size)
 PASS  __tests__/components/file14.test.tsx (150 MB heap size)
 PASS  __tests__/components/file15.test.tsx (150 MB heap size)
 PASS  __tests__/components/Header /file16.test.tsx (149 MB heap size)
 PASS  __tests__/components/file17.test.tsx (149 MB heap size)
 PASS  __tests__/utils/file18.test.tsx (150 MB heap size)
 PASS  __tests__/components/file19.test.tsx (149 MB heap size)
 PASS  __tests__/pages/file20.test.tsx (150 MB heap size)

如您所见,内存消耗更加一致。
您可以在this Github issue中阅读更多关于这个问题和解决方案的信息,它描述了jest的垃圾收集器泄漏问题。

rfbsl7qr

rfbsl7qr2#

您可以使用jest -w 1来避免这些内存问题。
有关Jest CLI documentation的更多信息
--最大工作者数=|#别名:-w。指定工作集区为执行测试而衍生的最大工作者数目。在单一执行模式中,这预设为计算机上可用的核心数目减去1(用于主执行绪)。在监看模式中,这默认为机器上一半的可用内核,以确保Jest不引人注目,并且不会使机器停机。在资源-但默认值应足以满足大多数使用情形。

shstlldc

shstlldc3#

在我的例子中,当我使用一个复杂对象时,就会发生这种情况:

expect(spyFn).toHaveBeenCalledWith(COMPLEX_OBJECT_WITH_DIFFICULT_STRUCTURE);

因此,当你使用jesttoEqual实用程序(它递归地比较对象的所有属性)时,你会得到 heap out of memory error
作为解决方案,尝试将“complexObject”替换为“simplifiedObject”:

  • 预期.objectContaining({...一些复杂对象属性})
  • 期望.any(对象)
vltsax25

vltsax254#

切换到节点版本16.10为我解决了这个问题。显然,jest的内存泄漏只发生在节点版本〉16的时候。我只是简单地使用NVM通过运行nvm install 16.10来首先安装节点16.10。在这样做之后,运行我的jest测试使用了555MB,而不是1.5GB的内存。

相关问题