how can i run a specific angular spec test file in NX workspace?
Asked Answered
A

6

51

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?

Archaeological answered 17/2, 2018 at 7:16 Comment(1)
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 slashFrazer
O
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'
Osullivan answered 26/2, 2018 at 6:2 Comment(3)
how to test it for a lib?Packston
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.Osullivan
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 libPackston
L
63

This worked for me

 nx test --test-file example.spec.ts
Lorenzalorenzana answered 9/10, 2020 at 17:40 Comment(3)
You can even be more specific and add --test-name-pattern=regex to target a specific test in that file.Manolete
You need to specify project name, (the workspace might have multiple projects). So like this: nx test projectName --test-file example.spec.tsShwa
Thank you. I needed this as well, so sharing: --codeCoverage flag.Panel
C
44

This worked for me:

nx run <project-name>:test --testFile=<file-name>
Cassock answered 10/11, 2021 at 9:42 Comment(0)
C
6

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")

Century answered 13/2, 2020 at 16:31 Comment(0)
O
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'
Osullivan answered 26/2, 2018 at 6:2 Comment(3)
how to test it for a lib?Packston
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.Osullivan
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 libPackston
S
3

this worked for me:

nx run <project-name>:test --testFile <filename.spec.ts>
Southwester answered 25/7, 2022 at 15:1 Comment(2)
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.Lacto
I don't see how this is any different from this answer https://mcmap.net/q/345103/-how-can-i-run-a-specific-angular-spec-test-file-in-nx-workspace (itself marginally different from the top voted answer).Whisper
C
1

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.

Coaster answered 24/7, 2023 at 9:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.