change this line

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

with this

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

to avoid the special character '.'

Answer from Bhanu on Stack Overflow
๐ŸŒ
Angular
angular.dev โ€บ guide โ€บ testing
Testing โ€ข Overview โ€ข Angular
The ng test command builds the application in watch mode and launches the Vitest test runner. ... โœ“ src/app/app.spec.ts (3) โœ“ AppComponent should create the app โœ“ AppComponent should have as title 'my-app' โœ“ AppComponent should render title Test Files 1 passed (1) Tests 3 passed (3) Start at 18:18:01 Duration 2.46s (transform 615ms, setup 2ms, collect 2.21s, tests 5ms)
๐ŸŒ
Angular
v2.angular.io โ€บ docs โ€บ ts โ€บ latest โ€บ guide โ€บ testing.html
Testing - ts - GUIDE
Open the 1st.spec.ts test file (Control/Command-P, then start typing the name of the file). Set a breakpoint in the test. Refresh the browser, and it stops at the breakpoint. ... An Angular component is the first thing most developers want to test.
Discussions

Angular 6 Unit testing : how to use test.ts for running karma tests
But that it not running , and it seems that it lacks some config in the karma.conf to run the test.ts More on stackoverflow.com
๐ŸŒ stackoverflow.com
unit testing - How to test a specific spec.ts file in Angular 4? - Stack Overflow
I am a novice in Angular. I am using Angular 4 in my current project.I was learning Unit testing with angular, just wanted to know if I can test a specific spec.ts file alone. Any help is appreci... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to run Angular 7 unit tests with custom environment.test.ts settings? - Stack Overflow
How do I get the Angular CLI to serve up the right environments/environment*.ts file in Angular 7? I've tried configuring the replacement of the environment.ts with environment.test.ts in three More on stackoverflow.com
๐ŸŒ stackoverflow.com
angular - Running a single test file - Stack Overflow
If you have multiple projects in your Angular project and/or are using NX, you can specify an Angular project to test: ... for something like path/to/your/component/componentname.spec.ts. This will scan every file in every project, and is slower. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74823058 โ€บ how-to-cover-the-test-ts-file-in-an-angular-project
How to cover the test.ts file in an Angular project
// This file is required by karma.conf.js and loads recursively all the .spec and framework files import 'zone.js/testing'; import { getTestBed } from '@angular/core/testing'; import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing'; declare const require: { context(path: string, deep: boolean, filter?: RegExp): { keys(): string[]; <T>(id: string): T; }; }; // First, initialize the Angular testing environment. getTestBed().initTestEnvironment( BrowserDynamicTestingModule, platformBrowserDynamicTesting(), { teardown: { destroyAfterEa
๐ŸŒ
GitHub
github.com โ€บ ui-router โ€บ sample-app-angular โ€บ blob โ€บ master โ€บ src โ€บ test.ts
sample-app-angular/src/test.ts at master ยท ui-router/sample-app-angular
// This file is required by karma.conf.js and loads recursively all the .spec and framework files ยท ยท import 'zone.js/dist/long-stack-trace-zone'; import 'zone.js/dist/proxy.js'; import 'zone.js/dist/sync-test'; import 'zone.js/dist/jasmine-patch'; import 'zone.js/dist/async-test'; import 'zone.js/dist/fake-async-test'; import { getTestBed } from '@angular/core/testing'; import { BrowserDynamicTestingModule, platformBrowserDynamicTesting ยท
Author ย  ui-router
๐ŸŒ
Reflect
reflect.run โ€บ articles โ€บ guide-to-testing-angular-apps
Guide to testing Angular apps - Reflect.run
December 1, 2022 - Open the accompanying src/app/app.component.spec.ts file that Angular had auto-created. It contains a describe block with 3 it tests. This is how we write test code - inside it blocks, inside a describe block.
๐ŸŒ
Testim
testim.io โ€บ blog โ€บ angular-component-testing-detailed-guide
Angular Component Testing: A Detailed How-To With Examples
June 18, 2025 - If they know the suite of unit tests will catch defects they might introduce, engineers wonโ€™t be afraid of refactoringโ€”i.e. changing the code without changing how it behavesโ€”which leads to a cleaner and more maintainable design. Test files should follow the name.component.spec.ts naming convention and should be located alongside the componentโ€™s other files. If you used the Angular CLI to create your Angular application, you might have noticed the app.component.spec.ts file.
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ angular โ€บ unit-testing-in-angular
Angular Unit Testing - Scaler Topics
March 7, 2023 - It would contain karma.config.js and src/test.ts(entry file for tests). ... Running the ng test command would trigger test.ts that has been configured inside the angular.json file.
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ angular unit testing tutorial with examples
Angular unit testing tutorial with examples - LogRocket Blog
November 19, 2024 - Fortunately, the Angular ecosystem created the ngentest package to automate the generation of test specs for each component, directive, and others. Assuming you wrote the code for your component and want to write a test for it, youโ€™ll have to install the ngentest package: ... $ gentest directive-name.ts -s # output to directive-name.spec.ts $ gentest pipe-name.ts # output to pipe-name.test.ts $ gentest service-name.ts.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 48315691 โ€บ how-to-test-a-specific-spec-ts-file-in-angular-4 โ€บ 48315759
unit testing - How to test a specific spec.ts file in Angular 4? - Stack Overflow
In Webstorms, you can right click ... in front of it and describe so you end up with fdescribe and fit. Then go to console or terminal then execute ng test....
๐ŸŒ
Testing-angular
testing-angular.com โ€บ angular-testing-principles
Angular testing principles โ€“ Testing Angular
February 17, 2021 - Each .spec.ts file represents a test. Typically, one .spec.ts file contains at least one Jasmine test suite (more on that in the next chapter).
๐ŸŒ
YouTube
youtube.com โ€บ watch
Understanding spec.ts Files in Angular | Angular Unit Testing Basics - YouTube
In this video, we take a quick look at the spec.ts testing file in Angular.This is where all unit tests live, and understanding its structure is essential be...
Published ย  December 12, 2025
๐ŸŒ
Angular
v17.angular.io โ€บ guide โ€บ testing
Angular
Angular is a platform for building mobile and desktop web applications. Join the community of millions of developers who build compelling user interfaces with Angular.
Top answer
1 of 3
14
  1. This was caused by Visual Studio Code "helping" me by importing: import { environment } from '@environments/environment.prod';
  2. The right place for fileReplacements is in architect.test.options.fileReplacements.
  3. In order to prevent VSCode from ever "helping" me like this again, I'm going to rename all of the environment.*.ts files other than environment.ts to something like DO-NOT-IMPORT.ENVIRONMENT.*.ts. At least that'll yell at me if I get the wrong import.
2 of 3
1

When you add a custom environment configuration, you need to add it also to the test element in the angular.json file, otherwise tests just run with the default environment configuration.

For instance, lets say you added an extra environment configuration named "customenv" to the already existing build.configurations element:

Copy"build": {
    ...

    "configurations": {
        "production": {
            "budgets": [
                {
                    "type": "initial",
                    "maximumWarning": "500kb",
                    "maximumError": "1mb"
                },
                {
                    "type": "anyComponentStyle",
                    "maximumWarning": "2kb",
                    "maximumError": "4kb"
                }
            ],
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                }
            ],
            "outputHashing": "all"
        },
        "development": {
            "buildOptimizer": false,
            "optimization": false,
            "vendorChunk": true,
            "extractLicenses": false,
            "sourceMap": true,
            "namedChunks": true
        },
        "customenv": {
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.customenv.ts"
                }
            ]
        }
    },
    "defaultConfiguration": "production"
},

At this point you can run

Copyng build --configuration=customenv

And then you might have also modified the already existing serve.configurations element like this:

Copy"serve": {
    ...
    "configurations": {
        "production": {
            "browserTarget": "myproject:build:production"
        },
        "development": {
            "browserTarget": "myproject:build:development"
        },
        "testws": {
            "browserTarget": "myproject:build:customenv"
        }
    },
    "defaultConfiguration": "development"
},

So now you can run:

Copyng serve --configuration=customenv

But if you try to run

Copyng test --configuration=customenv

You get the error:

An unhandled exception occurred: Configuration 'customenv' is not set in the workspace.

To be able to do that, you need to create a test.configurations element and add only the new custom environment:

Copy"test": {
    ...                  
    "configurations": {                        
        "customenv": {
            "fileReplacements": [
                {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.customenv.ts"
                }
            ]
        }
    },
๐ŸŒ
Medium
medium.com โ€บ @harshitaverma108 โ€บ angular-testing-basics-you-need-to-know-350cb6ce7951
Angular Testing Basics โ€” You need to know | by Harshitaverma | Medium
September 1, 2023 - It is a direct product of Angular JS Team, and they made it as default test runner for application created with the angular. (Default test runner for Angular) ... test.ts โ€” also known as test entry file, this file is used to load all spec.ts files ...
๐ŸŒ
Quora
quora.com โ€บ What-is-the-use-of-spec-TS-files-in-Angular
What is the use of spec.TS files in Angular? - Quora
Answer: So spec.ts essentially is a testing file where you can write your test cases for that specific component. This is configured in karma.json, which is the config file for testing and you might see the info in angular.json as well.
๐ŸŒ
Manning Publications
livebook.manning.com โ€บ book โ€บ angular-development-with-typescript-second-edition โ€บ chapter-14
Chapter 14. Testing Angular applications ยท Angular Development with Typescript, Second Edition
December 5, 2018 - Using the Jasmine framework for unit testing ยท Identifying the main artifacts from the Angular testing library ยท Testing services, components, and the router ยท Running unit tests against web browsers with the Karma test runner ยท End-to-end testing with the Protractor framework
๐ŸŒ
Medium
madhuwanthiaah.medium.com โ€บ the-first-step-to-write-angular-unit-test-cases-9e364d172eff
The First Step to Write Angular Unit Test Cases | by Hasini Madhuwanthi | Medium
February 21, 2021 - In this blog post, we are talking about the test case writing for components and services. Here, we have to write test cases only for a single unit, and it should be clean and understandable. The important thing is you need to know what do you expect after running the test case. Letโ€™s see what are the specs files first. Using spec files, we can write test cases for your source files. Angular applications have a .spec.ts file for each .ts file.