Oussama BEN MAHMOUD
Oussama BEN MAHMOUD

Reputation: 1482

Angular Unit Test - Run by module or folder

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'}
  ]
});

Upvotes: 16

Views: 13621

Answers (3)

Muthulakshmi M
Muthulakshmi M

Reputation: 787

Just add "f" in front of your need to run describe, like this

fdescribe('My pipe Testing', () => {
   it('', ()=>{
   });
});

Upvotes: -3

Swoox
Swoox

Reputation: 3750

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.

Upvotes: 14

anees
anees

Reputation: 344

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"
]
}

Upvotes: 1

Related Questions