Use jest "watch mode" with nx test (nx CLI)
Asked Answered
T

3

9

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?

Tenpin answered 7/3, 2020 at 16:32 Comment(0)
P
8

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
Paoting answered 17/4, 2020 at 16:24 Comment(0)
P
3

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

Pillage answered 22/12, 2022 at 3:55 Comment(0)
P
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!

Pedometer answered 20/7, 2021 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.