简体   繁体   English

在 Angular 15 配置 karma-firefox-launcher

[英]Configure karma-firefox-launcher in Angular 15

Angular 15 comes with fewer configuration files than the previous versions, incl. Angular 15 的配置文件比以前的版本少,包括。 karma.conf.js having been dropped. karma.conf.js已被删除。 For running ng test --browsers=FirefoxHeadless under Angular 14, I used to configure karma.conf.js to require('karma-firefox-launcher') plugin.为了在 Angular 14 下运行ng test --browsers=FirefoxHeadless ,我曾经将karma.conf.js配置为require('karma-firefox-launcher')插件。

How should this be done in Angular 15? Angular 15 这个应该怎么做?

  • is it ok to copy over the full karma.conf.js from the previous Angular 14 project and add set "architect.test.options.karmaConfig" in angular.json to that karma.conf.js file?是否可以从之前的 Angular 14 项目复制完整的karma.conf.js并将karma.conf.js中的集合"architect.test.options.karmaConfig"添加到该angular.json文件中?
  • or is there some other way to configure Karma plugins in angular.json directly, and would that be better?或者是否有其他方法可以直接在angular.json中配置 Karma 插件,这样会更好吗?
  • and can Angular 15 still help me by generating an initial karma.conf.js ? Angular 15 还能帮助我生成初始的karma.conf.js吗?

(motivation: my build server executes ng test in a headless container, so for the sake of a simple build container configuration, I prefer FirefoxHeadless over the test runner's default browser) (动机:我的构建服务器在无头容器中执行ng test ,所以为了简单的构建容器配置,我更喜欢FirefoxHeadless而不是测试运行器的默认浏览器)

You can do it manually like you described or you can use karma module to create configuration files from scratch, run: karma init karma.conf.js but only headless browser I found was ChromeHeadless (see image).您可以像您描述的那样手动完成,或者您可以使用 karma 模块从头开始创建配置文件,运行: karma init karma.conf.js但我发现只有无头浏览器是 ChromeHeadless(见图)。 karma - 选择浏览器配置步骤

More in the documentation: http://karma-runner.github.io/6.4/intro/configuration.html make sure you follow these steps before: http://karma-runner.github.io/6.4/intro/installation.html文档中的更多信息: http://karma-runner.github.io/6.4/intro/configuration.html确保您之前遵循以下步骤: http://karma-runner.github.io/6.4/intro/installation。 html

After adding a conf file, change angular.json:添加conf文件后,更改angular.json:

"test": {
  [...]
  "options": {
    [...]
    "karmaConfig": "karma.conf.js",
    [...]
  }
}

After that you can use ng test or npm test to run the tests.之后,您可以使用ng testnpm test来运行测试。

I think you should not add karma configuration direclty in angular.json (I didn't check if it is even possible)我认为你不应该在 angular.json 中直接添加业力配置(我没有检查它是否可能)

In Angular 15 you can generate the missing config by doing ng generate config karma then modify it to your needs.在 Angular 15 中,您可以通过执行ng generate config karma生成缺少的配置,然后将其修改为您的需要。 This also automatically updates the angular.json config file for you.这也会自动为您更新angular.json配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM