[Webpack 2] Use Karma for Unit Testing with Webpack

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 karma-webpack 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
  })
})

Github

时间: 2024-10-26 19:42:09

[Webpack 2] Use Karma for Unit Testing with Webpack的相关文章

[Unit Testing] AngularJS Unit Testing - Karma

Install Karam: npm install -g karma npm install -g karma-cli Init Karam: karma init First test: 1. Add test file to the karma.conf.js: // list of files / patterns to load in the browser files: [ 'test/hello.js' ], 2. Add test file: describe('First Un

Java Unit Testing - JUnit & TestNG

转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignificant programming notes...   |   HOME TABLE OF CONTENTS (SHOW) Java Unit Testing -  & TestNG 1.  Introduction to Unit Testing Framework The various type o

Unit Testing a zend-mvc application

Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in large projects, especially those with many people involved. Going back and manually testing every individual component of an application after every c

8 Principles of Better Unit Testing

结合工作中的实例,如何设计一个良好的Unit Test,不仅关系到程序的正确性,更关系到有效的缩短整个团队的开发周期(coding, build, refactoring),深刻的关系到敏捷在实际中的应用. 单元测试,是编程契约的一种重要体现.Unit Test应该相信别人会遵守契约.每个Project应该Cover住自己的行为,而不应该去测试别人的行为. Writing good, robust unit tests is not hard -- it just takes a little

Unit Testing PowerShell Code with Pester

Summary: Guest blogger, Dave Wyatt, discusses using Pester to analyze small pieces of Windows PowerShell code. Note   This is a five-part series that includes the following posts: What is Pester and Why Should I Care?Learn about a new test framework

Live Unit Testing

Live Unit Testing 相对于传统的Unit Test,VS2017 带来了一个新的功能,叫Live Unit Testing,从字面意思理解就是实时单元测试,在实际的使用中,这个功能就是可以在编写代码的时候进行实时的background的单元测试. 在体验之前,有几点注意事项是需要了解的: 1.目前 live unit tesing仅仅支持 C#和VB的传统.net版本,不支持.net core,当然,我觉得也不支持其他的语言,这点是暂时让我遗憾的,因为从体验的结果来看,如果能支持

hadoop编程小技巧(8)---Unit Testing (单元测试)

所需环境: Hadoop相关jar包(下载官网发行版即可): 下载junit包(最新为好): 下载mockito包: 下载mrunit包: 下载powermock-mockito包: 相关包截图如下(相关下载参考:http://download.csdn.net/detail/fansy1990/7690977): 应用场景: 在进行Hadoop的一般MR编程时,需要验证我们的业务逻辑,或者说是验证数据流的时候可以使用此环境,这个环境不要求真实的云平台,只是针对算法或者代码逻辑进行验证,方便调试

[Unit Testing] Mock a Node module's dependencies using Proxyquire

Sometimes when writing a unit test, you know that the module you're testing imports a module that you would like to observe, or at the very least mock to prevent side effects like network activity or file system operations. For JavaScript unit tests

[Mockito] Spring Unit Testing with Mockito

It is recommened to write unit testing with Mockito in Spring framework, because it is much faster with Spring framework test. But in case you can doing MVC, you still need to use spring framework test. In this post, we need to understand why to use