angularkarma-jasminekarma-runner

How to add a Karma plugin to an Angular 16 that has no karma.conf.js


I have an Angular projects which I created using the v16 CLI. All the unit tests locally fine using ng test.

I now wanted to set these up to run on out build machine that runs teamcity. In previous Angular projects (which I did not setup), they all have a karma.conf.js, and also a src/test.ts which has the comment

// This file is required by karma.conf.js and loads recursively all the .spec and framework files

In previous projects I have installed karma-teamcity-reporter, and then in the karma.confi.js I add require('karma-teamcity-reporter') to the plugins array, and we can then run this from team city using ng test --watch=false --browsers=ChromeHeadless --reporters=teamcity

However, when I went to do the same for my newer project, there is no karma.conf.js,or src/test.ts. If I search for another line like require('karma-jasmine') I find it no where.

I am not sure if in the older project someone (no longer here) added and setup the karma.conf.js themselves, but my question is how do I now add this karma-teamcity-reporter to my Angular project?


Solution

  • UPDATE

    Create the Karma configuration using ng generate config karma and/or edit your angular.json adding "karmaConfig": "karma.conf.js" under the test > options section and "builder": "@angular-devkit/build-angular:karma" under the test section.

    Example of test section in angular.json:

    {
      // ... more stuff
      "projects": {
        "project_ name": {
          // ... stuff
          "architect": {
            // ... stuff
            "test": {
              "builder": "@angular-devkit/build-angular:karma",
              "options": {
                "polyfills": [
                  "zone.js",
                  "zone.js/testing"
                ],
                "tsConfig": "tsconfig.spec.json",
                "inlineStyleLanguage": "scss",
                "assets": [
                  "src/favicon.ico",
                  "src/assets"
                ],
                "styles": [
                  "src/styles.scss"
                ],
                "scripts": [],
                "karmaConfig": "karma.conf.js"
              }
            }
          }
        }
      }
    }
    

    OLD ANSWER:

    You need to install the official CLI and then initialize the config file with init:

    $ npm install -g karma-cli
    $ karma init my.conf.js
    

    Reference: http://karma-runner.github.io/6.4/intro/installation.html and http://karma-runner.github.io/6.4/intro/configuration.html