DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Any Version of the Test Pyramid Is a Misconception – Don’t Use Anyone
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems
  • More Tests, More Confidence? Test Suites Are Investment Portfolios
  • One of Waterfall's Most Resilient Artifacts

Trending

  • How AI Is Actually Changing SRE Tools, Part 2: ITOps, Chaos Engineering, and the Rest of the Job
  • AI-Powered API Development With Spring AI
  • From Microservices to Agent Services: The Next Architectural Shift
  • Agents and Tools in Agentic AI: A Simple Explanation
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Running a Karma Test Case for a Single Spec File / Single module

Running a Karma Test Case for a Single Spec File / Single module

This quick tutorial shows how to run unit test cases on a single spec file when using Karma and Jasmine for your Angular unit tests.

By 
Shrisowdhaman Selvaraj user avatar
Shrisowdhaman Selvaraj
·
May. 23, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
80.0K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I'll explain the steps to run unit test cases for a single spec file and help developers complete Angular unit tests fast. There are many approaches to running Angular unit test cases for a single file, but I will explain the simplest one, which won't affect the code coverage for other team members' unit test cases.

Step 1

Add the Spec name in the test.ts file under the src folder. 

import 'zone.js/dist/zone-testing';
import { getTestBed } from '@angular/core/testing';
import {
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';

declare const require: any;

// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
  BrowserDynamicTestingModule,
  platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /Test-Demo\.spec\.ts$/);
// And load the modules.
context.keys().map(context);

Note: Test-Demo is nothing but Test-Demo.spec.ts.

Step 2

Run ng test --code-coverage 

Now Karma and Jasmine will check only Test-Demo.Spec.ts.

Before you push, you should run all the test cases and don’t push this file to the next repository.

Hope you enjoy. 


Step 3

Run Karma on module base 

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

unit test Test case

Opinions expressed by DZone contributors are their own.

Related

  • Any Version of the Test Pyramid Is a Misconception – Don’t Use Anyone
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems
  • More Tests, More Confidence? Test Suites Are Investment Portfolios
  • One of Waterfall's Most Resilient Artifacts

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook