'Set default launcher flags when I run ng test without additional arguments

I need to pass custom argument to chrome in order to set locale: --lang en-US

I've done it using customLauncher. My src/karma.conf.js:

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
    ],
    customLaunchers: {
      ChromeHeadlessLang: {
        base: 'ChromeHeadless',
        flags: [
          '--lang en-US', // ensure that the test works on all environments with the same locale
        ]
      }
    },
    browsers: ['Chrome'],
    ...
  });
};

And I can run it using

ng test --browsers ChromeHeadlessLang

However, I would like to use the lang flags by default, without specifying --browsers

ng test

Can I override the default flags, so that they are used when I run just ng test?



Solution 1:[1]

You can override the default behavour of any script in your package.json. Just add it to your scripts object like this:

"scripts": {
        "ng": "ng",
        "test": "ng test --watch=false --whateverParamYouWantToAddHere",
    },

Solution 2:[2]

You can specify default values for all parameters of ng test inside angular.json

e.g.:

 "test": {
          "builder": "ngx-build-plus:karma",
          "options": {
            "codeCoverageExclude": ["src/*.ts"],
            "codeCoverage": true,

All available parameters: https://angular.io/cli/test

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dave0688
Solution 2 Mateusz Budzisz