Tuesday, July 16, 2024

Running Jasmine Tests Using Karma and Azure Pipelines

In order to run Jasmine tests, the runner Karma must be used.  Karma requires Chrome.  So in Azure we will be installing all of that in a container in order to run the tests.  We will also be collecting coverage.  It is important that a full Linux distribution be used like Ubuntu.  We will be using the Azure pipeline wizard.

Install Jasmine

This will add Jasmine to your package.json

npm install --save-dev jasmine-core @types/jasmine @types/jasminewd2


Install Karma

This will add Karma to your package.json

npm install --save-dev karma karma-chrome-launcher karma-coverage karma-jasmine
karma-jasmine-html-reporter

Add a karma.conf.js to your project

Generating detailed code coverage reports can help you identify untested parts of your codebase. Add the coverageReporter to your karma.config.js

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: "",
    frameworks: ["jasmine", "@angular-devkit/build-angular"],
    plugins: [
      require("karma-jasmine"),
      require("karma-chrome-launcher"),
      require("karma-jasmine-html-reporter"),
      require("karma-coverage"),
      require("@angular-devkit/build-angular/plugins/karma"),
    ],
    client: {
      jasmine: {
        // you can add configuration options for Jasmine here
        // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html
        // for example, you can disable the random execution with `random: false`
        // or set a specific seed with `seed: 4321`
      },
      clearContext: false, // leave Jasmine Spec Runner output visible in browser
    },
    jasmineHtmlReporter: {
      suppressAll: true, // removes the duplicated traces
    },
    coverageReporter: {
      dir: require("path").join(__dirname, "./coverage/angularapp"),
      subdir: ".",
      reporters: [{ type: "html" }, { type: "text-summary" }],
    },
    reporters: ["progress", "kjhtml", "coverage"],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ["Chrome", "ChromeHeadless"],
    singleRun: false,
    restartOnFileChange: true,
  });
};


Add test and test-headless to your package.json

  "scripts": {
    "test": "ng test",
    "test-headless": "ng test --code-coverage --watch=false --browsers=ChromeHeadless",
  },

In Azure Define the Pipeline



Specify the Source Repository



Using Bash to Install Chrome




Here are the commands
sudo apt-get update
sudo apt-get install -y wget
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt-get install -y ./google-chrome-stable_current_amd64.deb
google-chrome --version

Use Node

Use the appropriate version of node for your project.





Use NPM to install the Angular CLI




Use NPM to install the node packages





Use NPM to run the build




Use NPM to run the tests








No comments:

Post a Comment