angular-cli - Use multiple karma.conf.js
Asked Answered
O

3

16

I am using angular-cli and I want to use two karma.conf.js files for testing.

  1. For CI : For which I am using Headless Chrome

    customLaunchers: {

      ChromeHeadless: {
        base: 'Chrome',
        flags: [
          '--headless',
          '--disable-gpu',
          // Without a remote debugging port, Google Chrome exits immediately.
          '--remote-debugging-port=9222',
        ],
      }
    }
    
  2. For Dev : For which I am using Chrome.

Is there any way that I could switch betwwen two karma.conf.js based on an argument value.

Osgood answered 30/8, 2017 at 5:6 Comment(0)
M
19

You can use option karmaConfig:

ng test --karmaConfig=another-karma.config.json

You may want to check ng test --help=true for other options.

Maletta answered 30/8, 2017 at 6:11 Comment(2)
the build doesn't bundle up the "Alternative Config File" and the CI process fails. Any way to include the other file in the bundle?Osgood
--karmaConfig= used to work with my angular 10 app. I switched to angular 14 and now the parameter is --karma-config=Cathar
D
24

In the Angular CLI v6 the option name got changed: ng test --karma-config <your config here>

Demars answered 21/5, 2018 at 14:8 Comment(1)
They're not saying to to use this over the accepted answer, just that in the latest version of Angular that the option changed from --config to --karma-config, other than that it is the exact same answer as the accepted answer. It probably could have just been a comment on the accepted answer instead of a separate answer but it's still useful information.Luxor
M
19

You can use option karmaConfig:

ng test --karmaConfig=another-karma.config.json

You may want to check ng test --help=true for other options.

Maletta answered 30/8, 2017 at 6:11 Comment(2)
the build doesn't bundle up the "Alternative Config File" and the CI process fails. Any way to include the other file in the bundle?Osgood
--karmaConfig= used to work with my angular 10 app. I switched to angular 14 and now the parameter is --karma-config=Cathar
E
0

To run a specific config file, you can use

karma start path/to/config_file/from/root

So you could make a file called karma.chromeheadless.js and one called karma.chrome.js and run with

karma start karma.chromeheadless.js
karma start karma.chrome.js

Even better than this, you could add some scripts in your package.json file

"scripts": {
  "chromeheadless": "karma start karma.chromeheadless.js",
  "chrome": "karma start karma.chrome.js",
},

and then run it with

yarn run chromeheadless
yarn run chrome

possibly replacing the word yarn with npm depending on your pacakage manager. The advantage of the second approach is that you don't need to install karma globally. And it could be a bit shorter to type. If you use yarn, I think you can even exclude the word run from the command.

Eboni answered 15/7, 2020 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.