kentcdodds / kentcdodds/kcd-scripts

Jest --findRelatedTests flag causes errors in precommit hook

Open
#245 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
889
Forks
205
PR merge metrics
No merged PRs in 30d

Description

I ran into this issue when trying to contribute to `@testing-library/jest-dom`: https://github.com/testing-library/jest-dom/issues/696 As of this writing, if you e.g. try to commit a change to that project's `README.md`, git will fail with this output:

```
husky > pre-commit (node v24.4.1)
✔ Preparing lint-staged...
⚠ Running tasks for staged files...
❯ node_modules/kcd-scripts/dist/config/lintstagedrc.js — 1 file
✔ README.md — 1 file
❯ *.+(js|jsx|json|yml|yaml|css|less|scss|ts|tsx|md|gql|graphql|mdx|vue) — 1 file
✔ kcd-scripts format
✔ kcd-scripts lint
✖ kcd-scripts test --findRelatedTests [FAILED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...

✖ kcd-scripts test --findRelatedTests:
● Validation Warning:

Unknown option "watchPlugins" with value ["/Users/trevor.burnham/code/jest-dom/node_modules/jest-watch-typeahead/build/file_name_plugin/plugin.js", "/Users/trevor.burnham/code/jest-dom/node_modules/jest-watch-typeahead/build/test_name_plugin/plugin.js"] was found.
This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation:
https://jestjs.io/docs/configuration

● Validation Warning:

Unknown option "watchPlugins" with value ["/Users/trevor.burnham/code/jest-dom/node_modules/jest-watch-typeahead/build/file_name_plugin/plugin.js", "/Users/trevor.burnham/code/jest-dom/node_modules/jest-watch-typeahead/build/test_name_plugin/plugin.js"] was found.
This is probably a typing mistake. Fixing it will remove this message.

Configuration Documentation:
https://jestjs.io/docs/configuration

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
```

It seems quite strange that a commit would be rejected because there are no related tests. The obvious fix would be to add the `--passWithNoTests` flag.

There's a deeper problem, though. At one point, I observed the same precommit hook failure as described above even when making changes to modules that _do_ have tests. I suspect that this is because Jest's `--findRelatedTests` has known compatibility issues with `roots` (https://github.com/jestjs/jest/issues/11527), which is a Jest configuration option [that kcd-scripts uses](https://github.com/kentcdodds/kcd-scripts/blob/530b9ea2055af62721831605a3616cbe722bf3fb/src/config/jest.config.js#L19).

For that reason, my recommendation would be to do away with the `--findRelatedTests` flag entirely. I've already submitted a PR to jest-dom to make that change there: https://github.com/testing-library/jest-dom/pull/709

Contributor guide

Open the contributing guide

Research direction

Start with src/config/jest.config.js, especially the roots configuration mentioned in the issue, and the kcd-scripts test command used by the lint-staged pre-commit hook. Reproduce the failure with a README.md-only change and with a tested module, then verify that the hook no longer rejects valid commits when no related tests are found.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
ci-cd, testing-qa, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.