I have Angular2 project build with Angular-CLI (beta 20).
Is there a way to run tests against only one selected spec file?
I used to have a project based on
Just small change need in test.ts
file inside src folder:
const context = require.context('./', true, /test-example\.spec\.ts$/);
Here, test-example
is the exact file name which we need to run
In the same way, if you need to test the service file only you can replace the filename like "/test-example.service"
This worked for me in every case:
ng test --include='**/dealer.service.spec.ts'
However, I usually got "TypeError: Cannot read property 'ngModule' of null" for this:
ng test --main src/app/services/dealer.service.spec.ts
Version of @angular/cli 10.0.4
Configure test.ts
file inside src
folder:
const context = require.context('./', true, /\.spec\.ts$/);
Replace /\.spec\.ts$/
with the file name you want to test. For example: /app.component\.spec\.ts$/
This will run test only for app.component.spec.ts
.
You can test only specific file with the Angular CLI (the ng
command) like this:
ng test --main ./path/to/test.ts
Further docs are at https://angular.io/cli/test
Note that while this works for standalone library files, it will not work for angular components/services/etc. This is because angular files have dependencies on other files (namely src/test.ts
in Angular 7). Sadly the --main
flag doesn't take multiple arguments.