git - 如何 Jest 使用 --changedSince 和 --onlyChanged?

标签 git unit-testing testing jestjs

我想使用 changedSinceonlyChanged Jest 标志。我更改了很多测试以及测试所依赖的文件。根据 jest cli 标志的文档:

changedSince: Runs tests related the changes since the provided branch. If the current branch has diverged from the given branch, then only changes made locally will be tested.

onlyChanged: Alias -o. Attempts to identify which tests to run based on which files have changed in the current repository. Only works if you're running tests in a git/hg repository at the moment and requires a static dependency graph (ie. no dynamic requires).

但是当我运行 jest --changedSince=masterjest --onlyChanged 时,它运行了 0 个测试,而根本没有在终端中提供任何内容。

jest --listTests 列出所有测试。但是,当我运行 jest --listTests --changedSince=masterjest --listTests --onlyChanged 时,它没有列出任何测试。

最佳答案

我发现 --changedSince--onlyChanged 也有点古怪。但是,您可以使用 lint-staged和 Jest 的 --findRelatedTests 并将其添加到预提交 Hook 。

--findRelatedTests {spaceSeparatedListOfSourceFiles}: 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. Can be used together with --coverage to include a test coverage for the source files, no duplicate --collectCoverageFrom arguments needed.

这是您需要在 package.json 文件中进行的更改。

{
  "scripts": {
    "test": "CI=true jest",
    "test:staged": "CI=true jest --env=jsdom --findRelatedTests"
  }
}

还有,

"lint-staged": {
  "src/**/*.js": [
    "test:staged",
    "git add"
  ]
}

这样,在每次提交时,Jest 将只对相关的源文件运行测试。不要忘记设置 CI=true 环境变量,否则您的测试将以监视模式运行并且永远不会结束。

如果你仍然想运行全套测试 npm test 就可以了。此外,当您推送到分支或发送 pull 请求时,最好有一个 CI 环境来运行所有测试。

关于git - 如何 Jest 使用 --changedSince 和 --onlyChanged?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787779/

相关文章:

unit-testing - 使用 Moq 设置测试的更好方法

testing - 有没有办法在一台机器上安装所有浏览器的所有版本?

linux - 将 --mirror push Git 存储库时出现重复文件错误

git - git status -s 的彩色全行

perl - 模拟另一个模块内部使用的 Git 模块

testing - Windows 应用程序的在线产品演示环境

java - 远程 JVM 机器人调用

Mac OS X v10.7 (Lion) 上的 Git

git-p4 克隆命令失败

unit-testing - 在 node.js 中测试响应?