Configure karma-firefox-launcher in Angular 15
Asked Answered
F

2

5

Angular 15 comes with fewer configuration files than the previous versions, incl. karma.conf.js having been dropped. For running ng test --browsers=FirefoxHeadless under Angular 14, I used to configure karma.conf.js to require('karma-firefox-launcher') plugin.

How should this be done in 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?
  • or is there some other way to configure Karma plugins in angular.json directly, and would that be better?
  • and can Angular 15 still help me by generating an initial 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)

Fireweed answered 30/11, 2022 at 8:11 Comment(0)
S
6

In Angular 15 you can generate the missing config by doing ng generate config karma then modify it to your needs. This also automatically updates the angular.json config file for you.

Scottiescottish answered 31/1, 2023 at 9:16 Comment(0)
E
2

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 - choose browsers config step

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

After adding a conf file, change angular.json:

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

After that you can use ng test or npm test to run the tests.

I think you should not add karma configuration direclty in angular.json (I didn't check if it is even possible)

Enthrall answered 30/11, 2022 at 17:23 Comment(1)
picked as "accepted answer", thanks! (but not 100% confident; Firefox and others should be available, but that again might still need editing config files instead of CLI ?- nothing against config files!)Fireweed

© 2022 - 2024 — McMap. All rights reserved.