Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude Folder (components) for unit testing in Angular 4 using Karma config?

I am using angular cli version 1.4.5 and below is the karma.conf.ts file

module.exports = function (config) {
  config.set({
  basePath: '',
  exclude: [
     "src/app/components/panel/panel.component.spec.ts",
     "src/app/components/accordion/accordion.component.spec.ts"
   ],
   frameworks: ['jasmine', '@angular/cli'],
   plugins: [
     require('karma-jasmine'),
     require('karma-chrome-launcher'),
     require('karma-jasmine-html-reporter'),
     require('karma-coverage-istanbul-reporter'),
     require('@angular/cli/plugins/karma')
   ],
   client:{
     clearContext: false 
   },
   coverageIstanbulReporter: {
     reports: [ 'html', 'lcovonly' ],
     fixWebpackSourcePaths: true
   },
   angularCli: {
     environment: 'dev'
   },
   reporters: ['progress', 'kjhtml'],
   port: 9876,
   colors: true,
   logLevel: config.DEBUG,
   autoWatch: true,
   browsers: ['Chrome'],
   singleRun: false
 });
};

and I even added the exclude in the tsconfig.spec.json file to exclude to pick those files for test.

{
 .....,
 "include": [
 "**/*.spec.ts",
 "**/*.d.ts"
 ],
 "exclude": [
     "app/components/panel/panel.component.spec.ts",
     "app/components/accordion/accordion.component.spec.ts"
 ]
}

is that am missing something to add for exclusion ?

like image 548
zulu Avatar asked Feb 06 '18 07:02

zulu


People also ask

How to exclude a component test file in an angular project?

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 exclude a component test file Let’s configure the my.component.spec.ts spec file to skip test case execution in test.js

How to run angular tests in karma?

An environment to run angular tests is being created using all the imports at the beginning of the file. TestBed is a powerful unit testing tool provided by angular, and it is initialized in this file. Finally, karma loads all the test files of the application matching their names against a regular expression.

How to test angular components with Jasmine test framework?

Let’s get started testing an Angular component with the Jasmine test framework. Karma is a test runner tool, it creates a browser instance, run tests to provide the expected results. The benefit of using Karma is that it can be operated via command line and It refreshes the browser automatically whenever we make even minor changes in our app.

Can Karma test exclude a single or group of Spec files?

It includes excluding a single or group of spec files from jasmine and mocha with the karma test runner. In my previous post, You learned different ways to [run a one spec.ts test case] (/angular-run-single-testfile) an Angular app.


1 Answers

Change the config in the src/test.ts.

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

https://github.com/angular/angular-cli/issues/9026#issuecomment-354475734

like image 59
Seven Avatar answered Oct 28 '22 22:10

Seven