5 ways to run a single test spec file in Angular app

In this tutorial, You learn different ways to run single test component file in angular application. This also includes running single spec file from jasmine and mocha with karma runner.

Usually, Angular application created with cli has a test.js file located in src folder This file contains below line of code which configures to include test files while running ng test or ng t command.

const context = require.context('./', true, /\.spec\.ts$/);

first parameters is all files of an angular project third parameter is include all spec files

Running ng test command executes all test spec files of an angular project

ng test 
ng t

if you have a my.component.spec.ts that you want to run single component test file for debugging and testing it.

There are multiple ways we can do

configure test.js to execute single spec file

Let’s configure component spec file in test.js

const context = require.context('./', true, /my.component\.spec\.ts$/);

This will run my.component.spec.ts file while running ng test command.

if you want to run a all test spec files inside a folder, you can configure like this

const context = require.context('./service', true, /my.component\.spec\.ts$/);

this will run all spec files under service folder

command line to run single test spec file

Angular cli provides --include option to include regular expression for including single or multiple files/folder.

ng test --include=**/my.component.spec.ts // single file
ng test --include'**/service/*.spec.ts // multiple files inside a folder


// single file
npm run test -- --include src/app/my.component.spec.ts

// all files inside service folder
npm run test -- --include src/app/service

npm script to single test spec file

In package.json, commands can be configured in scripts tag

    "scripts": {
        "singletest": "ng test --include=**/my.component.spec.ts"

This can be run with npm run singletest command

jasmine and karma allows to execute one component test spec

normally we have a describe for group of tests and it has single test method for writing unit testing in angular

describe('MyComponent', () => {
  it('should create', () => {
    //test for undefined 

Jasmine provides fdescribe and fit allows you to focus specific test cases to run it and skipped non focused test case execution

fdescribe('MyComponent', () => {
  fit('should create', () => {
    //test for undefined 

Please note that use ddescribe for jasmine version <2.1 version, and use fdescribe for >2.1 version.

How to run single component spec file with mocha and karma

mocha and karma allows only method to run single and multiple tests

  • describe.only - Allows to run group of tests
  • it.only - Allows to run a single test case
describe.only('MyComponent', () => {
  it.only('should create', () => {
    //test for undefined 


You learned multiple ways to run a single test or group of tests inside a folder.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.