7

I have an app that was generated with Nx CLI. I can run tests for this app with Jest using the the command nx test myApp. This works fine. However, I would like to use Jest's "watch mode". when running my tests, and can't figure out how to achieve this. Nx's documentation says simply:

To run unit tests for your application:

nx test myapp

I have been unable to find any more detailed documentation for this command, or how to add any other options/flags to it. Is there anyway to use watch mode with nx test?

Community
  • 1
  • 1
elethan
  • 16,408
  • 8
  • 64
  • 87

3 Answers3

7

TL;DR: yarn affected:test --all --parallel --maxParallel 10 --watch should work.

nx test actually runs ng test which in its turn is using the @nrwl/jest:jest builder that has a --watch option (in addition to some other jest specific options). You can see all the options by running nx test --help

Warning: If you have multiple apps/libs in your repository, this will only watch your default project's tests.

If you really want to watch all your projects tests, you will have to run:

yarn affected:test --all --parallel --maxParallel 10 --watch

  • --all we need this otherwise, this will not run tests on projects that you "affect" after running the command
  • --parallel because you want to run all the tests in parallel, otherwise this will only watch the first project in the list
  • --maxParallel because the default limit is 3 so if you have more than 3 projects, it will only watch tests for the 3 first projects
Younes
  • 199
  • 5
2

A simple command to run test for your applications with watch is

nx test <packageName> --watch

If you want to run test for a specific file in watch mode.

nx test <packageName> --testFile=<fileName> --watch

If you want to clear cache

nx test <packageName> --clearCache

saumyajain125
  • 93
  • 1
  • 4
0

If all your projects are using jest, there should be no issues with simply running:

jest --watch

Running adding a script for that and running it in the root directory works pretty well for me!

Dylan Watson
  • 2,283
  • 2
  • 20
  • 38