博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Webpack 2] Use Karma for Unit Testing with Webpack
阅读量:5928 次
发布时间:2019-06-19

本文共 3275 字,大约阅读时间需要 10 分钟。

When writing tests run by Karma for an application that’s bundled with webpack, it’s easiest to integrate webpack and Karma directly together. In this lesson we’ll see how to utilize the  plugin and reuse our existing webpack configuration to preprocess our test files with webpack.

 

karma.config.js:

const webpackEnv = {test: true}const webpackConfig = require('./webpack.config')(webpackEnv)const fileGlob = 'src/js/**/*.test.js'module.exports = function setKarmaConfig(config) {  config.set({    basePath: '',    frameworks: ['mocha', 'chai'],    files: [fileGlob],    preprocessors: {      [fileGlob]: ['webpack']    },    webpack: webpackConfig,    webpackMiddleware: {noInfo: true}, // no webpack output    reporters: ['progress'],    port: 9876,    colors: true,    logLevel: config.LOG_INFO,    autoWatch: false,    browsers: ['Chrome'],    singleRun: true,    concurrency: Infinity  })}

 

webpack.config.js:

const {resolve} = require('path')module.exports = env => {  return {    entry: './js/app.js',    output: {      filename: 'bundle.js',      path: resolve(__dirname, 'dist'),      pathinfo: !env.prod,    },    context: resolve(__dirname, 'src'),    devtool: env.prod ? 'source-map' : 'eval',    bail: env.prod,    module: {      loaders: [        {test: /\.js$/, loader: 'babel!eslint', exclude: /node_modules/},        {test: /\.css$/, loader: 'style!css'},      ],    },  }}

 

package.json:

{  "private": true,  "dependencies": {    "todomvc-app-css": "2.0.4",    "todomvc-common": "1.0.2"  },  "devDependencies": {    "babel": "6.5.2",    "babel-core": "6.8.0",    "babel-eslint": "6.0.4",    "babel-loader": "6.2.4",    "babel-preset-es2015-webpack": "6.4.1",    "babel-preset-stage-2": "6.5.0",    "chai": "3.5.0",    "cpy-cli": "1.0.0",    "css-loader": "0.23.1",    "eslint": "2.9.0",    "eslint-config-kentcdodds": "6.2.1",    "eslint-loader": "1.3.0",    "ghooks": "1.2.1",    "karma": "0.13.22",    "karma-chai": "0.1.0",    "karma-chrome-launcher": "1.0.1",    "karma-mocha": "1.0.1",    "karma-webpack": "1.7.0",    "mocha": "2.5.3",    "npm-run-all": "1.8.0",    "opt-cli": "1.4.2",    "rimraf": "2.5.2",    "style-loader": "0.13.1",    "webpack": "2.1.0-beta.7",    "webpack-dev-server": "2.0.0-beta",    "webpack-validator": "2.1.0"  },  "config": {    "ghooks": {      "pre-commit": "opt --in pre-commit --exec \"npm run validate\""    }  },  "scripts": {    "test": "karma start",    "watch:test": "npm test -- --auto-watch --no-single-run",    "validate": "npm-run-all --parallel validate-webpack:* lint",    "validate-webpack:dev": "webpack-validator webpack.config.js --env.dev",    "validate-webpack:prod": "webpack-validator webpack.config.js --env.prod",    "clean-dist": "rimraf dist",    "copy-files": "cpy src/index.html src/favicon.ico dist",    "clean-and-copy": "npm run clean-dist && npm run copy-files",    "prestart": "npm run clean-and-copy",    "start": "webpack-dev-server --env.dev --content-base dist",    "prebuild": "npm run clean-and-copy",    "prebuild:prod": "npm run clean-and-copy",    "build": "webpack --env.dev",    "build:prod": "webpack --env.prod -p",    "lint": "eslint ."  }}

 

test file:

import Controller from './controller'describe('controller', () => {  it('exists', () => {    expect(Controller).to.exist  })})

 

转载地址:http://rcevx.baihongyu.com/

你可能感兴趣的文章
基于事件驱动的DDD领域驱动设计框架分享(附源代码)
查看>>
Linux备份ifcfg-eth0文件导致的网络故障问题
查看>>
2018年尾总结——稳中成长
查看>>
行列式的乘法定理
查看>>
linux下内存释放问题
查看>>
LINQ之路12:LINQ Operators之数据转换(Projecting)
查看>>
SQL Server:数据库角色
查看>>
JFreeChart开发_用JFreeChart增强JSP报表的用户体验
查看>>
SpringMVC+Swagger详细整合
查看>>
计算机视觉领域最全汇总(第2部分)
查看>>
[译] 所有你需要知道的关于完全理解 Node.js 事件循环及其度量
查看>>
(六十九)复合语句
查看>>
我的友情链接
查看>>
Java Web中实现Servlet的方式
查看>>
第三方库之 - SVProgressHUD
查看>>
11个让你吃惊的 Linux 终端命令
查看>>
# 180111php编译错误
查看>>
js闭包
查看>>
度量时间差
查看>>
网络营销与电子商务
查看>>