我使用Jasmine和Karma为一个小型覆盖率应用程序编写了单元测试。并且Karma在这些测试中运行缓慢。
这是我的业力配置:
var unitTestReportOutputDir = 'unit-test-report';
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
reporters: ['dots'],
port: 9876,
colors: false,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
autoWatchBatchDelay: 300,
exclude: ['./test/data/*.js'],
files: [
'tests.webpack.js'],
preprocessors: {
'tests.webpack.js': ['webpack']
},
webpack: require('./webpack.config.js'),
webpackMiddleware: {
noInfo: true
},
htmlReporter: {
outputDir: unitTestReportOutputDir, // where to put the reports
focusOnFailures: true, // reports show failures on start
namedFiles: true, // name files instead of creating sub-directories
pageTitle: 'Unit Test Report', // page title for reports; browser info by default
urlFriendlyName: false, // simply replaces spaces with _ for files/dirs
reportName: 'test-summary', // report summary filename; browser info by default
// experimental
preserveDescribeNesting: false, // folded suites stay folded
foldAll: false, // reports start folded (only with preserveDescribeNesting)
}
});
}
这是我的webpack.config.js:
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var webpack = require('webpack');
var UglifyJsPlugin = require('uglify-js-plugin');
const path = require('path');
module.exports = {
devtool: 'inline-source-map',
entry: ['./src/index.js'],
output: {path: path.resolve(__dirname, "builds"), filename: 'bundle.js'},
module: {
rules: [
{
test: /\.jsx?$/,
use: [
{
loader: 'babel-loader'
}
]
},
{
test: /\.sass$/,
exclude: '/node_modules/',
use: ExtractTextPlugin.extract({
fallback: "style-loader",
loader: "css-loader!sass-loader"
})
}
]
},
resolve: {
extensions: ['.js', '.jsx', '.sass']
},
plugins: [
new ExtractTextPlugin({
filename: "stylesheets/style.css",
disable: false,
allChunks: true
}),
new webpack.ProvidePlugin({
"$": "jquery",
"jQuery": "jquery",
"window.jQuery": "jquery"
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compress: {
warnings: false
}
})
]
};
你们通常做什么来加速Karma的单位测试?
我把代码放在:https://github.com/zainulfranciscus/karma-test 每当我更新测试时,业力花了1分15秒开始和33秒运行测试。有35个单元测试
谢谢。
答案 0 :(得分:0)
你看过karma-sharding了吗?这个插件解决了一些浏览器的一些内存使用问题,以及众多或内存密集型规范。
它不会并行运行您的测试,但从版本4.0.0开始,它支持并行浏览器执行(即将对karma.conf.js
中列出的浏览器进行分片测试)
答案 1 :(得分:0)
在此处查看karma-parallel
插件:
https://www.npmjs.com/package/karma-parallel
与karma-webpack
插件相比,它更受欢迎且与karma-sharding
兼容。