javascript Karma --auto-watch 不再有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31428916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Karma --auto-watch no longer works
提问by TarkaDaal
My Karma installation used to auto-watch - when I saved a .js file, it'd re-run the tests. It's been a couple of months since I did any JavaScript, and now I come to use it again, the auto-watch feature isn't working. Here is my karma.conf:
我的 Karma 安装用于自动监视 - 当我保存 .js 文件时,它会重新运行测试。自从我做任何 JavaScript 以来已经有几个月了,现在我又来使用它了,自动监视功能不起作用。这是我的 karma.conf:
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: [
'jQuery/jquery-1.10.2.js',
'jasmine/jasmine.js',
'jasmine-jquery/jasmine-jquery.js',
'Angular/angular.js',
'Angular/angular-route.js',
'Angular/angular-mocks.js',
'Angular/angular-animate.min.js',
'Angular/angular-sanitize.min.js',
'Angular/angular-cache.min.js',
'emcommon.js',
'Moment/moment.js',
'ViewModels/Common/*.js',
'ViewModels/Settings/*.js',
'Tests/Common/*.js',
'Tests/Settings/*.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: {
'../ViewModels/**/*.js': 'coverage'
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage'],
coverageReporter: {
type: '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: true,
usePolling: 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
});
};
I've read and followed the advice here. I've tried setting usePolling
to true. I've used three different programs to save the file (VS, Sublime, and Notepad) to rule that out. If I stop Karma and restart it, it picks up the changes and they pass/fail as expected, but it will not see the files change while it's running.
我已阅读并遵循此处的建议。我试过设置usePolling
为true。我使用了三个不同的程序来保存文件(VS、Sublime 和记事本)来排除这种情况。如果我停止 Karma 并重新启动它,它会接收更改并且它们按预期通过/失败,但是在运行时它不会看到文件更改。
Moving from Karma 0.12.7 to 0.13.0 makes no difference to the problem.
从 Karma 0.12.7 迁移到 0.13.0 对问题没有影响。
回答by Kasper Lewau
Where's the output?
输出在哪里?
First of all, I think it would be of some help to see the output of a run with the following CLI options:
首先,我认为使用以下 CLI 选项查看运行的输出会有所帮助:
# add --single-run, or kill the process manually when finished.
karma start karma.conf.js --auto-watch --log-level debug > log.txt
Then paste the contents of log.txt
to a pastebin. It'd probably be too big to put as an edit to your question.
然后将 的内容粘贴log.txt
到pastebin。对您的问题进行编辑可能太大了。
A different browser, mayhaps?
可能是不同的浏览器?
I would also try to run a different browser than Chrome
, I wouldn't rule out that it maybe the cause of issue as every example in karma-runner#895shows Chrome
as the browser of choice (albeit this is an old issue and resolved by the looks of it).
我也会尝试运行与 不同的浏览器Chrome
,我不排除它可能是问题的原因,因为karma-runner#895中的每个示例都显示Chrome
为首选浏览器(尽管这是一个老问题并通过以下方式解决)它的样子)。
Give it a try with PhantomJS and/or Chrome Canary, and see if that does the trick.
用 PhantomJS 和/或 Chrome Canary 试一试,看看是否能奏效。
Go with a task-runner?
和任务执行器一起去?
Do you by any chance have a task runner as part of your local setup?
If so, you could killthe --auto-watch
and use an equivalent solution in the task runner of your choice.
您是否有机会将任务运行器作为本地设置的一部分?如果是这样,你可以杀了--auto-watch
,并使用您所选择的任务运行等效的解决方案。
The way we've set up our grunt task is as follows:
我们设置 grunt 任务的方式如下:
karma: {
options: {
configFile: 'karma.conf.js'
},
watch: {
background: false,
singleRun: false
}
}
With the following karma.conf settings:
使用以下 karma.conf 设置:
module.exports = function(config) {
config.set({
frameworks: ['mocha'],
basePath: '',
files: [ /** redacted **/ ],
urlRoot: '/base/app/',
reporters: ['progress'],
logLevel: config.LOG_INFO,
browsers: ['PhantomJS'],
singleRun: true
});
};
Try running with sudo
?
尝试运行sudo
?
Blank slate configuration
空白石板配置
Run karma init newconf.js
, then give this one a whirl:
运行karma init newconf.js
,然后试一试:
karma start newconf.js --auto-watch
I think seeing some output from your tests would be very helpful here. And preferably some version numbers of:
我认为在这里看到您的测试的一些输出会非常有帮助。最好是一些版本号:
- Chrome
- NodeJS
- Karma
- Test Framework (Mocha/Chai, Jasmine)
- 铬合金
- 节点
- 业力
- 测试框架(Mocha/Chai、Jasmine)
Edit#1
编辑#1
Please have a go with a karma.conf looking like the following:
请使用如下所示的 karma.conf:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'some_jasmine_spec.js',
],
reporters: ['progress'],
port: 9000,
colors: true,
logLevel: config.LOG_DEBUG,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false
});
};
Where the some_jasmine_spec.js
file would look like so:
当some_jasmine_spec.js
文件看起来像这样:
describe('dummy_test', function() {
it('just runs', function() {
expect(true).to.be.false;
});
});
Put the some_jasmine_spec.js
file in the same folder as this stripped down karma.conf and see if it yields a different result.
将some_jasmine_spec.js
文件放在与此剥离的 karma.conf 相同的文件夹中,看看它是否会产生不同的结果。
Edit#2
编辑#2
I just noticed that the last line in your files
array isn't being read.
If you look at the output of your first log file:
我只是注意到files
数组中的最后一行没有被读取。如果您查看第一个日志文件的输出:
# The last DEBUG [watcher] entry
# 27 - [36m17 07 2015 14:35:37.160:DEBUG [watcher]: [39mWatching "c:/Projects/Gazelle - EstateManager/DEV-Container/(9112) ViewDevice/EstateManagerUI/EstateManagerUI/Scripts/Tests/Settings"
# The last DEBUG [web-server] entry
# 102 - [36m17 07 2015 14:35:38.321:DEBUG [web-server]: [39mserving (cached): c:/Projects/Gazelle - EstateManager/DEV-Container/(9112) ViewDevice/EstateManagerUI/EstateManagerUI/Scripts/Tests/Settings/viewschedulemodule.tests.js
So, none of the files in the ./*/*.js
pattern are being read.
因此,没有./*/*.js
读取模式中的任何文件。
I would try to change it to ./**/*.js
, or even **/*.js
.
我会尝试将其更改为./**/*.js
,甚至**/*.js
.
Even so, the dummy jasmine test should've done the trick had it been related to files not being included.
即便如此,如果与未包含的文件相关,虚拟茉莉花测试应该已经完成了。
Edit #3
编辑 #3
Running low on ideas here but;
这里的想法很少,但是;
I would try changing the basePath
to ../
and removing ../
from all other file references. This is more so a scratching of my (itchy) curiosity than a 'valid' concern. But hey, tryingdoesn't hurt.
我会尝试更改basePath
为../
并../
从所有其他文件引用中删除。这更像是对我(发痒的)好奇心的一种抓挠,而不是“有效”的担忧。但是,嘿,尝试并没有什么坏处。
Edit #4
编辑 #4
Last ditch effort; Pop open a terminal and run the following (sorry, I'm on a UNIX based system - my MS-DOS isn't up to par, so to speak...).Let's clean it all out, kill the cache, reinstall the packages you require and give it another go.
最后一搏;弹出一个终端并运行以下命令(抱歉,我在基于 UNIX 的系统上 - 我的 MS-DOS 达不到标准,可以这么说......)。让我们把它全部清理干净,杀死缓存,重新安装你需要的包,然后再试一次。
Cleanup/out
清理/退出
$ rm -rf node_modules # clean out your local node modules
$ npm cache clean # clean out the npm cache
$ npm uninstall karma karma-cli karma-chrome-launcher karma-coverage karma-firefox-launcher karma-ie-launcher karma-jasmine jasmine -g # uninstall assorted karma/jasmine libraries globally
$ rm -rf node_modules # clean out your local node modules
$ npm cache clean # clean out the npm cache
$ npm uninstall karma karma-cli karma-chrome-launcher karma-coverage karma-firefox-launcher karma-ie-launcher karma-jasmine jasmine -g # uninstall assorted karma/jasmine libraries globally
Backup
备份
$ mv karma.conf.js karma.conf.bak.js # backup your karma.conf
$ mv karma.conf.js karma.conf.bak.js # backup your karma.conf
Reinstallation
重新安装
$ npm install karma karma-cli karma-chrome-launcher karma-coverage karma-firefox-launcher karma-ie-launcher karma-jasmine jasmine -g # reinstall assorted karma/jasmine libraries globally
$ npm install # reinstall your local node modules
$ npm install karma karma-cli karma-chrome-launcher karma-coverage karma-firefox-launcher karma-ie-launcher karma-jasmine jasmine -g # reinstall assorted karma/jasmine libraries globally
$ npm install # reinstall your local node modules
Reinitialisation
重新初始化
$ karma init # initialise a new karma.conf.js file
- make as few modifications as possible to the new karma.conf.js file
$ karma start karma.conf.js --auto-watch --log-level debug
- optionally, pipe the above out into a new
log.txt
and upload it. - this is so that we can compare the two and see if anything reallystands out.
- optionally, pipe the above out into a new
$ karma init # initialise a new karma.conf.js file
- 对新的 karma.conf.js 文件进行尽可能少的修改
$ karma start karma.conf.js --auto-watch --log-level debug
- 可选地,将上述内容输出到一个新的
log.txt
并上传。 - 这使我们可以比较两个,看看是否有任何真正脱颖而出。
- 可选地,将上述内容输出到一个新的
Debugging
调试
Ensure that singleRun
is set to false
in your new karma.conf.js file, then pop open the connected browser and go to http://localhost:9876
.
确保在新的 karma.conf.js 文件中singleRun
设置为false
,然后打开连接的浏览器并转到http://localhost:9876
。
Press the debug button, open up your dev tools (web inspector/console), and investigate what happens when you reload the page.
按下调试按钮,打开您的开发工具(Web 检查器/控制台),然后调查重新加载页面时会发生什么。
If this doesn't make any difference, I'm at a loss.
如果这没有任何区别,我会不知所措。
回答by TarkaDaal
For the record, I eventually fixed it by:
作为记录,我最终通过以下方式修复了它:
- using a new folder for my local node_modules
- using the git bash shell
- making sure the PYTHON environment variable was set using git bash style separators
- installing Karma version v0.12.0
- 为我的本地 node_modules 使用一个新文件夹
- 使用 git bash shell
- 确保使用 git bash 样式分隔符设置 PYTHON 环境变量
- 安装 Karma 版本 v0.12.0
None of it worked on its own, for me anyway. Hope this helps someone else.
无论如何,对我来说,它都没有单独起作用。希望这对其他人有帮助。