jest --findRelatedTests如何在后台工作? [英] How does jest --findRelatedTests work under the hood?
问题描述
查找并运行测试,这些测试涵盖了用空格分隔的源列表 作为参数传递的文件.对于预提交挂钩很有用 集成以运行最少数量的必要测试.
Find and run the tests that cover a space separated list of source files that were passed in as arguments. Useful for pre-commit hook integration to run the minimal amount of tests necessary.
这是官方文档,但是它如何工作?它是否分析项目中的所有导入,并且仅运行导入我要测试的文件的测试?我就是这样写的,但是它真的那样工作吗?
This is in official docs, but how does this work? Does it analyze all the imports in my project and only runs tests that import the file I want to test? That's how I would write it, but is it really working like that?
相关问题-查找相关测试时是否使用缓存?
Related question-does it use a cache when finding related tests?
推荐答案
在过去的几天里,我一直被同一个问题困扰.深入研究 Jest源代码,我想我对发生的事情非常了解.
I'v been stuck with the same question for the last few days. After digging through the Jest source code, I think I have a pretty good idea of what's going on.
运行--findRelatedTests path/to/src-code.js
时,首先发生的事情是Jest创建了内部包jest-resolve-dependencies
的实例.这是一个非常简单的类,具有两个方法:resolve
和resolveInverse
.
When running --findRelatedTests path/to/src-code.js
, the first thing that happens is Jest creates an instance of an internal package, jest-resolve-dependencies
. It's a pretty straightforward class with two methods: resolve
and resolveInverse
.
findRelatedTests
在您提供的路径上调用resolveInverse
,在我们的示例path/to/src-code.js
中查找需要该文件的每个源文件和测试文件.该查找直接依赖于某些Jest配置,特别是roots
和/或rootDir
,以帮助解析路径.
findRelatedTests
calls resolveInverse
on the paths you provided, looking up every source and test file that requires your file, in our example path/to/src-code.js
. This lookup relies directly on some Jest configuration, specifically roots
and/or rootDir
, to help resolve paths.
如果找到的文件是测试文件,则Jest运行它,非常简单.如果找到的文件是源文件,则将其命名为found-file.js
,则任何导入found-file.js
的测试文件和导入其自身导入found-file.js
的任何源文件的测试文件都将是.跑步.
If the found file is a test file, Jest runs it, simple enough. If the found file is a source file, call it found-file.js
, then any test files that import found-file.js
and the test files that import any of the source files that themselves import found-file.js
will be run.
正如维护者所说,这是传递逆相关性"解析器的巧妙实现.您可以在此while
循环.
It's a clever implementation of, as the maintainers put it, a resolver of "transitive inverse dependencies". You can see for yourself in this while
loop.
这篇关于jest --findRelatedTests如何在后台工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!