nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.17k stars 2.31k forks source link

Support passing a list of test files to the Jest executor #20462

Open cmparsons opened 9 months ago

cmparsons commented 9 months ago

Description

Similar to the existing testFile option, adding an option to the Jest executor that supports passing a list of tests files to run.

Motivation

The @nx/vite:test plugin seems to support this behavior already with testFiles, and it seems testFiles has replaced testFile, so this feature could bridge the functionality between the two test runners.

At Netlify, we use the circleci tests run command to split tests for parallelization and to utilize CircleCI's rerun failed tests feature. CircleCI's CLI will pass tests as a space-delimited list of files, so ideally we could easily hook into this behavior with a command like:

npx jest --listTests | circleci tests run --command="xargs npx nx run-many -t test --testFiles" --split-by=timings

Suggested Implementation

Add a new option called testFiles to the Jest executor that is either a string with the expectation that it's a space-delimited list of file names or to match the vitest plugin, an array of strings (I'm not sure how nx would parse this from the CLI).

Alternate Implementations

leosvelperez commented 3 months ago

hey @cmparsons! Thanks for the suggestion!

I'll look into adding support for this to the @nx/jest:jest executor. Please note that if you're using inferred tasks for Jest, you can already do this:

nx test <project> path/to/spec/file1 path/to/spec/file2
nx test <project> filename1 filename2
nx test <project> path/to/folder/containing/spec/files

The tasks inferred by the @nx/jest plugin run the Jest CLI directly jest from the project root as cwd. So, you're able to use anything the Jest CLI supports.