简体   繁体   中英

Timing function calls in Jest

I am trying to develop some code for my own library. I chose Jest as the testing framework. What I would like to do is have test cases to make sure that a particular function is not taking too much time to execute. Basically that requires some way of timing how much time a function call is taking. I am aware of the standard ways of measuring time . However, it may be nice to have some way of letting Jest do that work. I have tried looking into the (open and closed) issues on the Github Jest repo but I couldn't find anything that addresses my question in particular. Does anyone know of such a way of timing function calls with Jest?

You can always assert against time difference

const start = performance.now();
doStuff();
const end = performance.now();
expect(start - end).toBeLessThen(3000); // < 3s

Meanwhile I agree with @giuseppedeponte it's definitely not a purpose for unit-testing frameworks. Especially because there is no way to guarantee the same performance between different runs.

Do profiling locally once you change flow. Run function for significant amount of times against edge cases' data. Make your conclusions. And unit testing is expected to validate if code's logic is valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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