首页 > 解决方案 > Jest 应该只在预提交钩子的更改文件上运行

问题描述

--only-changed我有一个使用 jest 和标志设置的预提交挂钩。但是,有时即使我在单个文件中进行了更改,我的整个测试套件仍会运行(800 个测试!)。

我查看了其他一些jest标志,例如

     --lastCommit                  Run all tests affected by file changes in
                                    the last commit made. Behaves similarly to
                                    `--onlyChanged`. 
 

      --findRelatedTests            Find related tests for a list of source
                                    files that were passed in as arguments.
                                    Useful for pre-commit hook integration to
                                    run the minimal amount of tests necessary.

      --changedSince                Runs tests related to the changes since the
                                    provided branch. If the current branch has
                                    diverged from the given branch, then only
                                    changes made locally will be tested. Behaves
                                    similarly to `--onlyChanged`.       [string]

然而他们都有同样的问题。在进行一些挖掘时,我了解到 “如果找到的文件是测试文件, Jest运行它,很简单。如果找到的文件是源文件,则将其命名为 found-file.js,然后任何测试文件导入 found-file.js 和导入任何源文件的测试文件,这些源文件本身导入 found-file.js。 "

我正在做一个对我来说相对较新的项目。我想知道我是否有可能让我的预提交挂钩只运行编辑后的测试,而不是所有受影响的测试,或者我是否有办法追踪这棵“传递逆依赖”树并尝试解决不同进口或其他东西的问题。

这是尝试的一些输出示例--find-related-tests

Test Suites: 2 failed, 309 passed, 311 total
Tests:       2 failed, 803 passed, 805 total
Snapshots:   308 passed, 308 total
Time:        102.366 s
Ran all test suites related to files matching /\/Users\/me\/repo\/project\/src\/modules\/dogs\/components\/spots\/SpotsSpotter.tsx/i.

> @dogsapp/project@1.0.0 test:staged
> jest --findRelatedTests --passWithNoTests "/Users/me/repo/project/src/modules/dogs/components/spots/SpotsSpotter.tsx"

ERROR: "husky:lint-staged" exited with 1.
husky - pre-commit hook exited with code 1 (error)

当我只对一个文件进行简单更改时,它花费的时间太长了。任何人都知道我怎样才能找到为什么会这样?

似乎在这里为 --watch 标志解决了类似的问题:https ://www.gitmemory.com/issue/facebook/jest/8276/483695303

标签: reactjstestingjestjsts-jestpre-commit-hook

解决方案


推荐阅读