Jest: Detect infinite loops and stop tests

Created on 9 Apr 2020  路  9Comments  路  Source: facebook/jest

馃殌 Feature Proposal

Similar to Rome.

https://twitter.com/sebmck/status/1114672543133097984.
https://github.com/facebookexperimental/rome/blob/1add11d62d787ff646dcc52e8b35cb41c730a30e/packages/%40romejs/core/master/testing/TestRunner.ts#L342-L405

Motivation

Infinite loops can be hard to track down, we should try to help the user. We don't currently attach an inspector, but it shouldn't be _too_ much work doing so

Feature Request Help Wanted

Most helpful comment

I would love to work on it ! 馃殌

All 9 comments

I would love to work on it ! 馃殌

I started working on the feature by analysing Inspector API usage and how Rome is using it.

I made a schema to represent the process, to know if I was going in the right direction.
One of my questions concerns how we will make the monitorHeartbeat which pings the workers to check if they are still alive.
jest-infinite-loop (4)

Thanks for picking this up @ayshiff! monitorHeartbeat seems like it'd just me a message passed to the worker, when then responds with it's own message, sort of like ping -> <- pong. Another idea would be to make workers automatically emit a heartbeat e.g. every second, and the parent process could detect when there has been no message for n seconds (another PARENT_MESSAGE)

Another idea could be to keep track of loops with more than N iterations as @gaearon did here.
Maybe we could try a mix of both ideas ? I don't know what is the best way to go !

That could work, but I think ideally we'd want something with the inspector, I don't think we want to rely on some babel plugin to run

Another downside of Babel plugin is you have to hardcode a number of iterations.

I've been stuck in my progress for a moment.

This is where I am:

  • Initialize the inspector and connect a session to it. Then we can enable the Debugger.
  • Inside jest-worker I created a new message PARENT_MESSAGE_HEARTBEAT that is emitted each time the child process聽has work to do. When there has been no message received from the child for N time, we consider that the worker is dead and we call the Debugger.pause and wait for the Debugger.paused response. Once we get the response, we can retrieve the stack trace.

The monitorHeartbeat is not working properly at the moment. In fact, I was wondering if you could give some explanations about the way the jest-worker package is working. I don't want to have misunderstandings on important parts of the code.

I will need some help on this one 馃憤

Hi @ayshiff and sorry this went under the radar for months. It sounds like you already got quite far and it'd be a shame to lose all this work. I was not involved in writing jest-worker (neither child processes nor worker threads) but I landed a major refactor of the way the workers terminate last year, so I can possibly help / we can figure things out together. I didn't catch any specifics from your comment yet, but if you're still interested, let me know what problem you ran into :)

Hi @jeysal !
No problem, it's been a long time since I looked at my code.
I'll take a look at what I did and I'll make a Draft PR with some explanations of what I tried to do (and surely some questions) which will be much more simple for you to review 馃憤

Was this page helpful?
0 / 5 - 0 ratings