简体   繁体   English

将 jest 与 web3 和 ganache 一起使用时测试很慢

[英]Tests slow when using jest with web3 and ganache

I have an end-to-end test using jest that deploys several Ethereum contracts to ganache via web3 and communicates with them.我有一个使用 jest 的端到端测试,它通过 web3 将几个以太坊合约部署到 ganache 并与它们通信。

The test is very slow on jest, taking over a minute when running it directly without jest takes 10 seconds. jest 的测试非常慢,如果直接运行它而不用 jest 需要 10 秒,则需要 1 多分钟。 I created a new clean project that isolates this test, it's the only test in the project.我创建了一个新的干净项目来隔离这个测试,它是项目中唯一的测试。

I followed the jest troubleshooting guide and tried all the performance related tips, like --runInBand , yet they had negligible effect.我遵循了开玩笑的故障排除指南并尝试了所有与性能相关的技巧,例如--runInBand ,但它们的效果可以忽略不计。

Jest runs inside a VM which makes crypto calculations very slow. Jest 在虚拟机中运行,这使得加密计算非常慢。 Since web3 is heavy with crypto calculations, signatures and such, this overhead kills the test.由于 web3 包含大量加密计算、签名等,因此这种开销会扼杀测试。

The solution is adding the following to the jest config file:解决方案是将以下内容添加到 jest 配置文件中:

"extraGlobals": ["Math"]

For more info, see https://jestjs.io/docs/en/configuration#extraglobals-arraystring有关详细信息,请参阅https://jestjs.io/docs/en/configuration#extraglobals-arraystring

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM