Run nx test for chosen file paths

329 Views Asked by At

My project has thousands of test files and I wanted to make a dynamic mechanism of splitting those files on Gitlab CI using parallel jobs.

I wrote a script that splits *.spec.ts(x) files into groups of X tests, but I'm not sure how I can use those paths to start only given ones.

Project is placed in Nx monorepo and under the hood I'm using Jest.

I tried two commands:

nx test my-project --testFile="apps/my-project/file1.spec.ts,apps/my-project/file2.spec.ts"
nx test my-project --grep="apps/my-project/file1.spec.ts|apps/my-project/file2.spec.ts"

Unfortunately, it always runs all the tests in my project completely ignoring the parameters.

My Nx version:

$ nx report
 >  NX   Report complete - copy this into the issue template

   Node   : 18.17.1
   OS     : linux-x64
   yarn   : 1.22.19
   
   nx                 : 16.10.0
   @nx/js             : 16.10.0
   @nx/jest           : 16.10.0
   @nx/linter         : 16.10.0
   @nx/workspace      : 16.10.0
   @nx/cypress        : 16.10.0
   @nx/devkit         : 16.10.0
   @nx/esbuild        : 16.10.0
   @nx/eslint-plugin  : 16.10.0
   @nx/nest           : 16.10.0
   @nx/node           : 16.10.0
   @nx/react          : 16.10.0
   @nrwl/tao          : 16.10.0
   @nx/web            : 16.10.0
   @nx/webpack        : 16.10.0
   typescript         : 4.4.3
1

There are 1 best solutions below

0
On

The only solution for now is to use:

export TEST_FILES=/src/file1.spec.ts,/src/file2.spec.ts,/src/file3.spec.ts

nx test frontend --testPathPattern=\"$TEST_FILES\"

This solution works for both root relative and abslotute paths. Unfortunately, you can't achieve the same using nx run.