Search code examples
javascriptreactjsjestjsbitbucket-pipelines

Is there a way to reproduce a flaky test in jest?


I work on a React application with a Bitbucket pipeline that runs its tests (with jest). There's a test that sometimes fails, sometimes doesn't and I want to fix it but I can't really check if my solutions work. Does someone know a way to reproduce a flaky test failure?


Solution

  • Flaky tests, no matter the language, are caused by unexpected changes in the environment in which the test runs.

    If you can reproduce the failure locally and in isolation, it is likely due to the test referencing a non-deterministic part of the environment, like system time, a random number, or a network connection.

    If you can reproduce the failure locally, but not in isolation, it is likely that one test is leaking state into the global environment that causes the failing test to break.

    And, if you cannot reproduce the failure locally, regardless of order, you might be dealing with a race condition between two tests that access the same shared global state while executing in parallel.

    I wrote a more detailed answer to a similar question, here.