I have an Angular project with Karma setup for unit test. The test and coverage works fine for all spec files, but from day to day the app gets bigger and it bothers me to run all tests, even if I'm focusing in a new module, so I wanted to run only some tests that I want to check, for example, I have a pipes folder and all I want to do is to run only the spec files in this folder, I know I can update the context in my test.ts file but I don't want to update and revert that file each time...
I tried to create a new Karma config file and updated the files
property to add only the files I want but it didn't work. I don't know maybe I'm doing it wrong. (See below a portion of the code in this file)
Is there any solution or trick to do this? something like a separate karma config (ex: karma.config.pipes.ts
) file that alters the context ?
// Override dev config
config.set({
files: [
{pattern: '../app/pipes/*.spec.ts'}
]
});
Running ng test command executes all test spec files of an angular project if you have a my.component.spec.ts that you want to run single component test file for debugging and testing it. Let’s configure component spec file in test.js This will run my.component.spec.ts file while running ng test command.
When we run the npm test or ng test , Angular takes the configuration from angular.json. Look at the test part of an angular.json file. We are specifying test.ts is our main file and karma.conf.js is our karma configuration file
By writing a unit test for your blocks (components, services, etc.), you can easily detect when there is a break. Our example Angular app has a service, a component, and an async task to simulate data being fetched from the server. How do you write an Angular test?
Jasmine is the default testing framework recommended by the Angular documentation. The Angular CLI sets up Jasmine for you, so you don’t have to install it separately. Karma is the default test running for Angular. The AngularJs team created it after having difficulties testing Angular with the tools that existed at the time.
Not sure if it's relevant anymore but as of 8.1 you can use the --include
option for example:
"ng test --include=**/folder/*.spec.ts"
Sources: https://angular.io/cli/test#options and https://blog.ninja-squad.com/2019/07/03/angular-cli-8.1/
Also make sure to update your: @angular-devkit/build-angular.
u can have test.ts file which contains following code
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.
// here u can specify the folder
const context = require.context("./pipes", true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
and configure this file in tsconfig.spec.ts
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"baseUrl": "./",
"module": "commonjs",
"target": "es5",
"types": [
"jasmine",
"node"
]
},
"files": [
"test.ts"
],
"include": [
"**/*.spec.ts",
"**/*.d.ts",
"./polyfills.ts"
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With