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-代码.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.
正如维护者所说,这是一个传递逆依赖"解析器的巧妙实现.您可以在 this 中亲自查看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屋!