Skip to content

Add --related-to flag to run only tests affected by changed files#3470

Open
stevenmini2019 wants to merge 1 commit into
avajs:mainfrom
stevenmini2019:related-tests
Open

Add --related-to flag to run only tests affected by changed files#3470
stevenmini2019 wants to merge 1 commit into
avajs:mainfrom
stevenmini2019:related-tests

Conversation

@stevenmini2019

Copy link
Copy Markdown

Summary

Implements #1986 by adding a --related-to <files> CLI flag that runs only the test files which (transitively) import the given source files — the AVA equivalent of jest --findRelatedTests.

This is intended for pre-commit hooks (e.g. lint-staged) where you only want to run the tests that could be affected by the files that changed, instead of the whole suite.

npx ava --related-to src/foo.js
npx ava --related-to src/foo.js src/bar.js
npx ava --related-to 'src/**/*.js'

How it works

  • A new lib/related-tests.js exposes createRelatedTestSelector(relatedTo, projectDir).
  • It walks the static import graph of every discovered test file (resolving relative/absolute specifiers, following transitive imports, and skipping bare package specifiers as external dependencies).
  • It builds a reverse map source file → test files and keeps only the test files connected to any of the files passed to --related-to.
  • The selector is wired into lib/cli.js as the existing testFileSelector hook on Api#run, so it composes with other patterns/--match filters.

Notes / limitations

  • Only relative and absolute import specifiers are followed; bare (package) imports are treated as external and do not connect a test to a project source file.
  • Type-only imports (import type {…}) are followed as well, which can make the selection slightly over-inclusive but never under-inclusive.
  • The selection respects any other patterns or filters passed, so --related-to can be combined with a glob or --match.

Test plan

  • test/related-tests/test.js unit-tests the selector: single-file selection, multi-file selection, transitive imports, interaction with an existing selection filter, empty input, and space/comma-separated inputs.
  • Verified end-to-end: ava --related-to <source> runs only the related test file(s) out of the full discovered set, including the transitive case where src/foo.js imports src/bar.js.

Implements avajs#1986. The new `--related-to <files>` CLI flag runs only the test
files that (transitively) import the given source files, mirroring
`jest --findRelatedTests`. It builds an import graph for the discovered test
files and selects those connected to the changed sources, which makes it useful
for pre-commit hooks (e.g. lint-staged) that only want to run affected tests.

- New lib/related-tests.js with createRelatedTestSelector()
- Wired into lib/cli.js as a testFileSelector
- Unit tests under test/related-tests/
- Documented in docs/05-command-line.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant