How does the --findRelatedTests joke work under the hood?
Find and run those tests that cover the source list, space-separated files passed as arguments. Useful for commit before commit to run the minimum number of tests required.
It's in the official docs, but how does it work? Does it parse all imports in my project and run tests that import the file I want to test? This is how I write it, but does it really work?
A related question: Does it use the cache when looking for related tests?
source to share
I have been stuck with the same question for the past few days. After looking at the Jest source code , I think I have a pretty good idea of ββwhat's going on.
When you start the --findRelatedTests path/to/src-code.js
first thing that happens, - Jest creates an instance of the inner package jest-resolve-dependencies
. It is a fairly simple class with two methods: resolve
and resolveInverse
.
findRelatedTests
calls resolveInverse
for the paths you specify, looking at every source and test file that requires your file, in our example path/to/src-code.js
. This search relies directly on some Jest configurations, specifically roots
and / or rootDir
to help resolve paths.
If the file it finds is a test file, Jest runs it fairly easily. If the file found is a source file, name it found-file.js
, then there will be found-file.js
all test files that import found-file.js
and test files that import any of the source files that import themselves found-file.js
.
This is a clever implementation of what the maintainers say is a "transitive inverse dependency" solver. You can see for yourself in this in the cycle .
source to share