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
Newsletter
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
  • Testing Business Programs Without Constructing Domain Objects
  • The Math Behind AI Testing: Why 1,000 Test Cases May Tell You Less Than 100
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems

Trending

  • Part 1: Building Governed MCP Tool Services With Quarkus LangChain4j and Goose
  • OpenCode: Setting Up an Open-Source AI Coding Assistant
  • To Switch, or Not to Switch the AI Model
  • How You Clear Your HTML5 Canvas Matters

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
May. 23, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
80.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Testing Business Programs Without Constructing Domain Objects
  • The Math Behind AI Testing: Why 1,000 Test Cases May Tell You Less Than 100
  • Why Standard Test Automation Misses the Failures That Matter in AI Agent Systems

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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