Tests are failing when running together but pass individually using jest and puppeteer

Viewed 4728

So i wrote some tests that are wrapped with describe using jest and puppeteer. Every suit is basically saved as js file and contains multiple tests inside and i noticed the when i run multiple suites using “npm run test” some tests are failing but when i run every suite individually all tests inside pass all the time. Any idea what could be the root cause for that behavior? Maybe something that needs to be refreshed in the “beforeEach” stage? 10x

1 Answers

The root cause for this is (almost) always side effects, that is your checks are not independent on each other.

The solution is in how you think about those checks. Typically, you want:

  • the checks to be independent
  • to be as small as possible
  • each check checks only one thing, that translates into having one assertion in it
  • shared steps are in before or beforeEach hooks
  • you take shortcuts when doing stuff on the UI, that is you use API calls, you set your cookies, local storage etc. rather than just clicking on the UI

Doing any of these and ideally all of them will help you improve your UI checks, usually so much that you'll get rid of flaky and long-running monstrosities.

Related