如何使用Karma配置在Angular 4中排除文件夹(组件)以进行单元测试? [英] How to exclude Folder (components) for unit testing in Angular 4 using Karma config?

查看:129
本文介绍了如何使用Karma配置在Angular 4中排除文件夹(组件)以进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是角度cli版本1.4.5和以下版本的karma.conf.ts文件

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

,我什至在tsconfig.spec.json文件中添加了排除以排除以选择这些文件进行测试.

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 ?

推荐答案

更改src/test.ts中的配置.

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

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

这篇关于如何使用Karma配置在Angular 4中排除文件夹(组件)以进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆