我正在尝试使用karma运行js测试用例。但总是得到插件未找到错误。有趣的部分是相同的配置文件,对我的其他同事工作正常
这是日志:
$ karma start karma.conf.js
04 10 2016 17:51:24.755:ERROR [plugin]: Cannot find plugin "karma-babel-preprocessor".
Did you forget to install it?
npm install karma-babel-preprocessor --save-dev
04 10 2016 17:51:24.769:ERROR [plugin]: Cannot find plugin "karma-mocha".
Did you forget to install it?
npm install karma-mocha --save-dev
04 10 2016 17:51:24.778:ERROR [plugin]: Cannot find plugin "karma-chrome-launcher".
Did you forget to install it?
npm install karma-chrome-launcher --save-dev
04 10 2016 17:51:24.791:ERROR [preprocess]: Can not load "babel", it is not registered!
Perhaps you are missing some plugin?
/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:9
throw error('No provider for "' + name + '"!');
^
Error: No provider for "framework:mocha"! (Resolving: framework:mocha)
at error (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:22:12)
at Object.parent.get (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:9:13)
at get (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:54:19)
at /usr/local/lib/node_modules/karma/lib/server.js:143:20
at Array.forEach (native)
at Server._start (/usr/local/lib/node_modules/karma/lib/server.js:142:21)
at invoke (/usr/local/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15)
at Server.start (/usr/local/lib/node_modules/karma/lib/server.js:103:18)
at Object.exports.run (/usr/local/lib/node_modules/karma/lib/cli.js:280:26)
at Object.<anonymous> (/usr/local/lib/node_modules/karma/bin/karma:3:23)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
这是karma.config.js
// Karma configuration
// Generated on Thu Jan 28 2016 12:33:59 GMT-0500 (EST)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
plugins: [
'karma-babel-preprocessor',
'karma-mocha',
'karma-chrome-launcher',
],
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai'],
// list of files / patterns to load in the browser
files: [
'.venv/lib/python2.7/site-packages/xstatic/pkg/jquery/data/jquery.min.js',
'.venv/lib/python2.7/site-packages/horizon/static/horizon/js/horizon.js',
'.venv/lib/python2.7/site-packages/horizon/static/horizon/js/horizon.quota.js',
'.venv/lib/python2.7/site-packages/horizon/static/horizon/js/horizon.instances.js',
'myapp/static/comp/js/pricing.js',
'myapp/test/js/**/*.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: {
'myapp/test/js/**/*.js': ['babel']
},
babelPreprocessor: {
options: {
presets: ['es2015'],
sourceMap: 'inline'
},
filename: function (file) {
return file.originalPath.replace(/\.js$/, '.es5.js');
},
sourceFileName: function (file) {
return file.originalPath;
}
},
// 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: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
这是package.json
{
"name": "comp_horizon",
"version": "1.0.0",
"description": "some description",
"main": "index.js",
"scripts": {
"test": "node node_modules/karma/bin/karma start karma.conf.js --single-run"
},
"repository": {
"type": "git",
"url": "git+https://github.com/priverepo/comp_horizon.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/priverepo/comp_horizon/issues"
},
"homepage": "https://github.com/priverepo/comp_horizon#readme",
"devDependencies": {
"babel": "^6.3.26",
"babel-core": "^6.4.5",
"babel-loader": "^6.2.1",
"babel-preset-es2015": "^6.3.13",
"chai": "^3.4.1",
"karma": "^0.13.19",
"karma-babel-preprocessor": "^6.0.1",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.0.0",
"karma-coverage": "0.3.1",
"karma-mocha": "^1.2.0",
"mocha": "^3.1.0",
"webpack": "^1.12.12",
"webpack-bundle-tracker": "0.0.93"
},
"dependencies": {
"babel-polyfill": "^6.3.14"
}
}
对于无法文件插件错误,我正在安装该插件。但错误仍然存在。例如因为找不到插件&#34; karma-babel-preprocessor&#34;,我做了npm install karma-babel-preprocessor --save-dev。它找到了。但仍然有错误。
由于
答案 0 :(得分:1)
“ npm install mocha --save-dev”对我有用
答案 1 :(得分:0)
我用karma-requirejs
安装了npm i -D karma-requirejs
业力抱怨找不到该模块
直到我尝试了
plugins: [
require("karma-requirejs")
]
在karma.conf.js中,我看到了错误堆栈的踪迹:karma-requirejs不包括requirejs
作为依赖项。
我用npm i -D requirejs
答案 2 :(得分:-1)
在尝试运行测试之前,您是否尝试过运行npm install?你的同事是否有任何全局安装的npm软件包可能允许这个配置为他们工作而不是你?我的全局包缓存(在Windows 10上)是C:\ Users \\ AppData \ Roaming \ npm。如果他们安装了你没有安装的东西,请尝试全局安装(在npm install上使用-g选项),然后在项目目录中运行npm install,然后尝试再次运行测试。