我正在使用this repo代码在app中应用browserify。我想为我的应用程序编写单元测试,包括karma和karma browserify。我的文件夹结构是:
karma.config.js
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' , 'browserify'],
// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.js',
'node_modules/angular-router/angular-router.js',
'node_modules/angular-mocks/angular-mocks.js',
'app/js/main.js',
'app/js/specs/main.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: {
'app/js/specs/main.spec.js': [ 'browserify' ]
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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,
browserify: {
watch: true,
debug: true,
plugin: ['karma-browserify']
}
})
}
的package.json
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ghazanfar Khan",
"license": "ISC",
"dependencies": {
"angular": "^1.6.7",
"angular-animate": "^1.6.7",
"angular-mocks": "^1.6.7",
"angular-route": "^1.6.7",
"bower": "^1.8.2",
"gulp": "^3.9.1",
"gulp-browserify": "^0.5.1",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.1",
"gulp-connect": "^5.0.0",
"gulp-jshint": "^2.0.4",
"gulp-minify-css": "^1.2.4",
"gulp-uglify": "^3.0.0",
"jasmine-core": "^2.8.0",
"jshint": "^2.9.5",
"karma": "^1.7.1",
"karma-browserify": "^5.1.2",
"karma-chrome-launcher": "^2.2.0",
"karma-jasmine": "^1.1.1",
"run-sequence": "^2.2.0",
"watchify": "^3.9.0"
}
}
当我执行业力开始时,它会给我错误
错误:插件karma-browserify导出了一个对象,期望一个函数 在Browserify.plugin(C:\ Users \ User \ Desktop \ sample app \ UnitTestApp \ node_modules \ browserify \ index.js:457:28)
我很困惑通过使用angular.mocks.module方式或在编写测试时使用require for module mock来编写单元测试代码。需要一个简单的例子来根据这个结构编写单元测试。