Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute only one test spec with angular-cli

People also ask

How do I run a single spec ts file?

ts file have all its tests grouped in describe block like this: describe('SomeComponent', () => {...} You can easily run just this single block, by prefixing the describe function name with f : fdescribe('SomeComponent', () => {...}

How do I run a specific test in Jasmine?

You can use fit() or fdescribe() instead of it() and describe() to achieve what is known as focussed test cases. describe("test spec", function() { it("test case 1", function() { }); fit("test case 2", function() { }); }); In the above example, only test case 2 would get executed.

Which one is used to run the unit tests in Angular?

Karma. Karma is a test runner for JavaScript. Along with Jasmine, Karma is one of the default testing tools for Angular.


Each of your .spec.ts file have all its tests grouped in describe block like this:

describe('SomeComponent', () => {...}

You can easily run just this single block, by prefixing the describe function name with f:

fdescribe('SomeComponent', () => {...}

If you have such function, no other describe blocks will run. Btw. you can do similar thing with it => fit and there is also a "blacklist" version - x. So:

  • fdescribe and fit causes only functions marked this way to run
  • xdescribe and xit causes all but functions marked this way to run

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.


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


use --include

it's work with --include

ng test --include src/app/path-to-component/path-component.component.spec.ts

I tried --main but it shows all fail results while it's not. I think --main will change the main test.ts file.