Karma+Jasmine+istanbul+webpack 自动化单元测试

Karma+Jasmine+istanbul+webpack 自动化单元测试

1.webpack 可以解决需要使用 require的问题(nodejs: require is not defined)

2.jasmine 异步方法 async/await: 用于http请求并得到返回值(nodejs http 请求返回undefined)
https://jasmine.github.io/tutorials/async

3.webpack 之后覆盖率会有问题 覆盖率不准确,需要引入karma-coverage-istanbul-reporter 并在karma-config配置 webpack

4.it 块之间异步,执行顺序混乱, 如果需要有执行顺序的话, 把先要执行的放在 beforeAll里

dependencies

“dependencies”: {
“jasmine”: “^3.5.0”,
“request”: “^2.88.0”
},
“devDependencies”: {
“istanbul-instrumenter-loader”: “^3.0.1”,
“jasmine-ts-console-reporter”: “^3.1.1”,
“karma”: “^4.4.1”,
“karma-chrome-launcher”: “^3.1.0”,
“karma-coverage”: “^2.0.1”,
“karma-coverage-istanbul-reporter”: “^2.1.1”,
“karma-jasmine”: “^2.0.1”,
“karma-sourcemap-loader”: “^0.3.7”,
“karma-webpack”: “^4.0.2”,
“webpack”: “^4.41.2”
},
“scripts”: {
“test”: “karma start”
},

karma config

https://jasmine.github.io/api/edge/global

// Karma configuration
// Generated on Mon Dec 16 2019 16:12:16 GMT+0800 (China Standard Time)

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
  //
  { pattern: 'test/*_test.js', watched: false }
],


// list of files / patterns to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
  'test/*_test.js': ['webpack','sourcemap','coverage']
},
webpack: {
  // karma watches the test entry points
  // (you don't need to specify the entry option)
  // webpack watches dependencies
  // webpack configuration
  node: {
    console: true,
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  },
  devtool: 'inline-source-map',
        module: {
            rules: [
                {
                    test: /\.js$/,
                    enforce: 'pre',
                    use: 'istanbul-instrumenter-loader',
                    exclude: /node_modules/
                }
                /* loaders */
            ]
        }
},

webpackMiddleware: {
  // webpack-dev-middleware configuration
  // i. e.
  stats: 'errors-only',
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage-istanbul'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
coverageIstanbulReporter: {
  // reports can be any that are listed here: https://github.com/istanbuljs/istanbuljs/tree/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib
  reports: ['html', 'lcovonly', 'text-summary'],

  // base output directory. If you include %browser% in the path it will be replaced with the karma browser name
  dir: 'coverage/',

  // Combines coverage information from multiple browsers into one report rather than outputting a report
  // for each browser.
  combineBrowserReports: true,

  // if using webpack and pre-loaders, work around webpack breaking the source path
  fixWebpackSourcePaths: true,

  // Omit files with no statements, no functions and no branches from the report
  skipFilesWithNoCoverage: true,

  // Most reporters accept additional config options. You can pass these through the `report-config` option
  'report-config': {
    // all options available at: https://github.com/istanbuljs/istanbuljs/blob/aae256fb8b9a3d19414dcf069c592e88712c32c6/packages/istanbul-reports/lib/html/index.js#L135-L137
    html: {
      // outputs the report in ./coverage/html
      subdir: 'html'
    }
  }
}

})
}

参考

https://jasmine.github.io/api/edge/global
https://www.cnblogs.com/wushangjue/p/4539189.html
https://blog.zeromake.com/pages/karma-webpack-istanbul/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值