【问题标题】:Jest - Find related tests and run them on `pre-commit` HookJest - 查找相关测试并在 `pre-commit` Hook 上运行它们
【发布时间】:2020-11-02 20:21:26
【问题描述】:

我有 Jest 作为我的测试运行者。我的提交钩子里也有哈士奇。我想在每次提交、受我的更改和相关更改影响的测试上运行。

我在 Jest 的文档中看到了以下选项,但我在使它们起作用方面没有取得进展:

--findRelatedTests -> To find the related test to my changes 
--testPathPattern -> To execute tests only on specific folders

您是否有文档或任何可能有用的示例。或者向我解释我怎么能做到这一点。?谢谢!!

【问题讨论】:

  • findRelatedTests 到底有什么困惑?文档对此非常精确,您需要向其提供已更改文件的列表。我希望它类似于 github.com/typicode/husky/issues/412 ,即 jest --findRelatedTest $(git status --porcelain | grep M | awk '{print $2}') ,如果您以 Windows 为目标,它将无法工作。你检查过 Jest onlyChanged 吗?好像是这样的。

标签: git jestjs diff test-runner husky


【解决方案1】:

查看packages/jest-cli/src/cli/args.ts我们可以找到:

throw new Error(
  'The --findRelatedTests option requires file paths to be specified.\n' +
    'Example usage: jest --findRelatedTests ./src/source.js ' +
    './src/index.js.',
);

我知道它可能在某些情况下工作(它在我的一个项目中工作并且在另一个项目中不工作,配置非常接近但版本不同),所以请记住,即使它不起作用 - 它仍然可能不是因为配置错误或类似原因。

testPathPattern 通常通过package.jsonjest 部分指定 -> testMatch 喜欢:

"testMatch": ["**/?(*.)+(spec).js"]

【讨论】:

    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2014-09-19
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    相关资源
    最近更新 更多