以下为 Karma 配置的一些详细代码。
1.文件结构
│ .gitignore
│ karma.conf.js
│ package.json
│ README.md
│ yarn.lock
│
├─coverage
│ │ lcov.info
│ │
│ ├─html
│ │ base.css
│ │ index.html
│ │ prettify.css
│ │ prettify.js
│ │ sort-arrow-sprite.png
│ │ sorter.js
│ │ utils.js.html
│ │
│ └─lcov-report
│ base.css
│ index.html
│ prettify.css
│ prettify.js
│ sort-arrow-sprite.png
│ sorter.js
│ utils.js.html
│
├─src
│ utils.js
│
└─test
utils.spec.js
2.package.json
{
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-istanbul": "^4.1.5",
"babel-preset-env": "^1.6.1",
"install": "^0.10.1",
"istanbul-instrumenter-loader": "^3.0.0",
"jasmine-core": "^2.8.0",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage-istanbul-reporter": "^1.3.0",
"karma-jasmine": "^1.1.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-webpack": "^2.0.5",
"webpack": "^3.8.1"
},
"dependencies": {}
}
3.karma.conf.js
// Karma configuration
// Generated on Sun Nov 05 2017 16:19:33 GMT+0800 (中国标准时间)
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: [
'./src/**/*.js',
'./test/**/*.spec.js',
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'./src/**/*.js':['webpack'],
'./test/**/*.spec.js':['webpack']
},
webpack:{
module: {
rules: [{
test: /\.js$/,
use: {
loader: 'istanbul-instrumenter-loader',
options: { esModules: true }
},
enforce: 'pre',
exclude: /node_modules|\.spec\.js$/,
},
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: ['env'],
plugins: ['istanbul']
}
},
exclude: /node_modules/
}]
}
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['coverage-istanbul'],
coverageIstanbulReporter: {
reports: ['html', 'text-summary'],
dir: 'coverage/',
fixWebpackSourcePaths: true,
skipFilesWithNoCoverage: true,
'report-config': {
html: {
subdir: 'html'
}
}
},
// 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: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
4.utils.js
function add(x){
return(y)=>{
return x + y
}
}
export {
add
}
5.utils.spec.js
import { add } from "../src/utils"
describe("测试算数运算",()=>{
it("测试加法运算",()=>{
const add5 = add(5)
expect(add5(5)).toBe(10)
})
})
完。