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

In this tutorial, You learn different ways to run a single test component file in an angular application. This also includes running a 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$/);

The first parameter is all files of an angular project the third parameter includes 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 a single component test file for debugging and testing it.

There are multiple ways we can do

configure test.js to execute a single spec file

Let’s configure component spec file in test.js

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

Running the ng test command, execute the test cases in the my.component.spec.ts file.

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

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

this will run all spec files under the service folder

command line to run single test spec file

Angular CLI provides the --include option to include a regular expression for including single or multiple files/folders.

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 configure in the scripts tag

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

It run run with the npm run singletest command

jasmine and karma allows executing 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 a 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.