javascript 是否可以在没有浏览器的情况下运行 Karma?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21895984/
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
Is it possible to run Karma with no browsers at all?
提问by randwa1k
I started an Angular.js app with Yeoman's yo angular
that includes Karma testing. Then, the grunt test
reasonably failed because Karma can't find any browsers. (The browsers
has not been set in the app's node_modules/karma config file.)
我使用 Yeoman's 启动了一个 Angular.js 应用程序yo angular
,其中包括 Karma 测试。然后,grunt test
合理地失败了,因为Karma 找不到任何浏览器。(browsers
尚未在应用程序的 node_modules/karma 配置文件中设置。)
I'm doing my development via SSH into a remote machine, which I think (let me know if I'm wrong) is pointless to have Chrome, /usr/bin/chromium-browser
, installed.
我正在通过 SSH 开发远程机器,我认为(如果我错了,请告诉我)/usr/bin/chromium-browser
安装Chrome 是毫无意义的。
So is it possible to run Karma without any browsers?
那么是否可以在没有任何浏览器的情况下运行 Karma?
采纳答案by Mik378
Karma needsa browser to be set.
Karma需要设置浏览器。
You can make use of PhantomJS instead of Chrome.
Indeed, it's more discreet than a traditional browser launch.
您可以使用 PhantomJS 而不是 Chrome。
事实上,它比传统的浏览器启动更谨慎。
回答by Clay
I am going to add my two cents to this.
我要加上我的两分钱。
Correct - Karma requires a browser to run. BUT - you can run Chrome in Headless mode, which means although you do need the browser installed, it will not open it's UI, and you can therefore run the tests purely through an SSH session for example.
正确 - Karma 需要浏览器才能运行。但是 - 您可以在 Headless 模式下运行 Chrome,这意味着虽然您确实需要安装浏览器,但它不会打开它的 UI,因此您可以纯粹通过 SSH 会话来运行测试。
We used this configuration for our CI/CD deployments. Our Docker image for running the tests had Chrome installed and we ran them with Chrome headless mode. Worked like a charm.
我们将此配置用于 CI/CD 部署。我们用于运行测试的 Docker 映像安装了 Chrome,我们以 Chrome 无头模式运行它们。像魅力一样工作。
To use this, simply modify your browsers
property in your karma.conf.js
要使用它,只需browsers
在您的karma.conf.js
browsers: ['ChromeHeadless']
Hope this might help someone out there who may be looking for something similar...
希望这可以帮助那些可能正在寻找类似东西的人......
回答by avi.elkharrat
This question and answer is very relevant as of today (soon 2018, > angular2, @angular/cli, typescript, ...).
截止到今天,这个问题和答案非常相关(即将在 2018 年,> angular2,@angular/cli,typescript,...)。
Here is a small update, based on what i found as useful on the net:
这是一个小更新,基于我在网上发现的有用信息:
Say you have an angular cli
generated project that has not been tempered with. Say you want to use PhantomJS
to run your angular2 tests (nothing shocking).
假设您有一个angular cli
未经调整的生成项目。假设你想用它PhantomJS
来运行你的 angular2 测试(没什么大惊小怪的)。
Start by installing PhantomJS
launcher for Karma in your project
首先PhantomJS
在您的项目中为 Karma安装启动器
npm i --save-dev karma-phantomjs-launcher
Next you have to update the karma.conf.js
file as follows:
接下来,您必须karma.conf.js
按如下方式更新文件:
First the the plugins properties:
首先是插件属性:
plugins: [
require('karma-jasmine'),
require('karma-phantomjs-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('@angular/cli/plugins/karma')
],
Then the browser properties
然后浏览器属性
browsers: [ 'PhantomJS' ],
Running the test at this point, you will probably stumble on the following error:
此时运行测试,您可能会偶然发现以下错误:
PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
TypeError: pre,template,textarea,script,style is not iterable!
at http://localhost:9876/_karma_webpack_/polyfills.bundle.js:792
Basically, that means your PhantomJS
needs different polyfills. Uncomment the following lines in your src\polyfills.ts
基本上,这意味着您PhantomJS
需要不同的 polyfill。取消注释以下行src\polyfills.ts
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';
Last but not least, here are the links that helped me work this out:
最后但并非最不重要的是,以下是帮助我解决此问题的链接:
How to run jasmine tests without browser ?