0

I have a Node.js application which I'm testing with Mocha on a fast(ish) dev machine. I've noticed that sometimes the fast CPU will mask some errors. If the tests are run on a machine with a slower CPU these errors starting showing up.

Question: Is there an easy way to temporarily slow down or simulate a slow down in CPU processing to surface these errors? Or way to run these tests at full speed and still discover this type of error?

4

1 回答 1

1

这些差异的一个可能原因是某些函数可能需要更长的时间来运行,具体取决于它们运行的​​机器,例如,如果它涉及大量计算或从数据库读取。这可能会修改调用回调的顺序。

要解决这个问题,您可以通过在测试中使用(例如) Sinon.js来更好地控制并行操作序列的运行顺序:它具有出色的 spy/stub 功能,并且还提供了假计时器

通过模拟(存根)需要时间运行的异步函数,您可以消除速度因素(取决于机器)。此外,假计时器允许控制包装在setTimeoutsetInterval

于 2014-11-10T18:00:57.510 回答