I found that there is a little bit of confusion and misunderstanding around how things like parallelization work in jest, which sometimes leads to additional hacking around problems that may not exist or speculating incorrectly about test failure. This is also of course a point of concern when you have code that for some reason or another uses global variables. Here are a short summary of things that may cause confusion.
Simple example, the global variable r is included in the test condition, but it is accurately run in all cases because the tests are not run in parallel.
This test will take 3 seconds, and will accurately count the global variable. If it was in parallel, it may only take 1 second, and would inaccurately count the global variable due to race conditions
Let's take another example where we use a global variable, and then two different tests use the global variable.
file_using_some_globals.js
test_global_vars1.test.js
test_global_vars2.test.js
This test completes in less than 2 seconds, and these tests are run in parallel. They use different instances of the global state, and therefore have no worries with colliding their state.
While seeking the fabled "squawk-less" test, it is often useful to mock console so that tests that produce an expected error don't actually print an error message. However, if not done carefully, you will remove errors across tests
So, could a mock from one test affect another test? If it's in the same file, yes!
mock_console.test.js
To properly mock these, you should restore the console mock at the end of your function
Your test output should just be a big list of PASS statements, not interleaved with console.error outputs from when you are testing error conditions of your code
"Squawkless tests" is a term I made up, but it means that if you have code under test that prints some errors to the console, then mock the console.error function, as in the previous section. Don't stand for having a bunch of verbose errors in your CI logs! However, I also suggest only mocking out console.error for tests that are expected to have errors, lest you paper over unexpected errors.
Figure: a nice clean test suite without a bunch of crazy console.error outputs
Getting better at testing requires exercise, and understanding the basics of your tools can help! Hopefully this helps you achieve a better understanding and write cleaner jest tests.