It would be useful to be able to specify a 'global' type of beforeEach and afterEach that would apply across tests in every test file. There are things I need to do before and after each test in every test file I have and have to have the before/afterEach in every one of my test files, which is not ideal.
@rightaway have you seen the setupFiles option?
These files will before each test suite. Since each test suite is sandboxed you may not need a teardown
You can put beforeEach/afterEach in setupTestFrameworkScriptFile, setupFiles runs before injecting Jest's globals.
It's not possible to save some context in beforeEach in the setupTestFrameworkScriptFile file that can be accessed in each test?
You can write some side effects to file system, but the context is not shared, as Jest runs your tests in sandboxed VMs – because of that we can parallelize stuff safely.
Thanks for the explanation. I couldn't find info on how Jest parallelizes tests, does it run tests within each test file sequentially but runs test files in parallel? Or does it even run tests within test files in parallel? Is there a way to configure how many it does in parallel, or does Jest decide that by itself?
Test files are run in parallel, test functions inside a file are run sequentially. By default Jest spawns N-1 workers, where N is your CPU cores. In watch mode it's cut by half. You can specify -w=x flag for spawning x parallel node workers (each running in its own thread) or -i to run with just one worker.
Most helpful comment
You can put beforeEach/afterEach in setupTestFrameworkScriptFile,
setupFilesruns before injecting Jest's globals.