2015-03-19 88 views

回答

35

您可以在這裏使用幾種技術:karma使用minimatch globes作爲文件路徑,並且使用可以利用它來排除某些路徑。

作爲第一個解決方案我想說嘗試將文件的唯一路徑與覆蓋範圍添加到預處理:

// karma.conf.js 
module.exports = function(config) { 
    config.set({ 
    files: [ 
     'src/**/*.js', 
     'test/**/*.js' 
    ], 

    // coverage reporter generates the coverage 
    reporters: ['progress', 'coverage'], 

    preprocessors: { 
     // source files, that you wanna generate coverage for 
     // do not include tests or libraries 
     // (these files will be instrumented by Istanbul) 
     'src/**/*.js': ['coverage'] 
    }, 

    // optionally, configure the reporter 
    coverageReporter: { 
     type : 'html', 
     dir : 'coverage/' 
    } 
    }); 
}; 

上面的一個是因果報應覆蓋默認的例子,它表明,只有那些src文件夾中的文件將被預處理。

另一個技巧可以使用!操作,以排除特定路徑:

preprocessors: { 
    // source files, that you wanna generate coverage for 
    // do not include tests or libraries 
    'src/**/!(*spec|*mock).js': ['coverage'] 
}, 

的一個上面,使覆蓋範圍來看,只有對那些不以spec.jsmock.js結束JavaScript文件。同理也可應用於文件夾來完成:

preprocessors: { 
    // source files, that you wanna generate coverage for 
    // do not include tests or libraries 
    'src/**/!(spec|mock)/*.js': ['coverage'] 
}, 

不處理在specmock夾中的任何JavaScript文件。

+0

是的,這正是我所需要的,感謝您的幫助。 – 2015-03-22 00:29:16

+3

提示:如果它們位於src/spec和src/mock文件夾中,則將排除所有spec和mock文件夾,然後使用如下所示:''src /!(spec | mock)/ **/*。js': [ '覆蓋']' – 2015-09-09 13:47:09

相關問題