44

is there any way to run an specific angular spec test file in Nx work space?

Recently i moved my Angular 4 cli application into Nx work space. Previously i used fdescribe to achieve this functionality.
So please suggest me if there is any option to do this in Nx work space?

Raj Kumar
  • 583
  • 2
  • 5
  • 9
  • There's a few similar/duplicate answers so I'll just comment here a couple of things that caught me out here: 1. You don't need to specify the full/relative path to the file; you can just use the filename. 2. If you do specify a path, it doesn't work with backslash (Windows-style), you must use forward slash – rom99 Jun 15 '23 at 10:48

6 Answers6

58

This worked for me

 nx test --test-file example.spec.ts
AzizStark
  • 1,390
  • 1
  • 17
  • 27
33

This worked for me:

nx run <project-name>:test --testFile=<file-name>
David Sentiurin
  • 430
  • 4
  • 5
5

For me the watch tag did the magic:

ng test --watch

Before running through everything it offered me the following options:

 › Press a to run all tests.
 › Press f to run only failed tests.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.

Selecting the 'p' option and typing xy.service.spec.ts worked fine.

Note: I'm using not the latest version ("@nrwl/nx": "7.4.0")

Marcell Kiss
  • 538
  • 3
  • 11
4

this worked for me:

nx run <project-name>:test --testFile <filename.spec.ts>
Amir Meyari
  • 573
  • 4
  • 9
  • 30
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](https://stackoverflow.com/help/how-to-answer). – Tyler2P Jul 26 '22 at 14:23
  • 4
    I don't see how this is any different from this answer https://stackoverflow.com/a/69910939/2540278 (itself marginally different from the top voted answer). – Robin Goupil Sep 22 '22 at 10:19
3

It would be the same way we do it for a single app, but please note you should use --app='your-app-name' along with ng test, so the final command looks like below,

ng test --app='app1'
Sriram
  • 179
  • 9
  • 1
    how to test it for a lib? – Paritosh Apr 26 '18 at 12:59
  • 2
    Libs cannot be tested individually, we can add fdescribe to the spec file in lib and run test on the app that uses the lib. – Sriram Apr 27 '18 at 17:08
  • Got the solution. I had to add an `app`. Angular cli expects main.ts. through the app consuming the lib, we can execute the tests for the lib – Paritosh Apr 27 '18 at 17:32
0

It's actually nx run <project-name>:test --testFile=<pathtofile.spec.ts>

Use Unix style for path (/ instead of ).

You can run with --watch option if you want the process to keep running tests every time you make a code change.

arunkumaraqm
  • 337
  • 1
  • 13