Skip to content
Roger Xu edited this page Jun 13, 2016 · 4 revisions

Karma

Karma

Configuration

File Patterns

  • exclude
  • files
  • preprocessors

Examples:

  • **/*.js: All files with a "js" extension in al subdirectories
  • **/!(jquery).js: Same as previous, but excludes "jquery.js"
  • **/(foo|bar).js: In all subdirectories, all "foo.js" or "bar.js" files

Files

Type: Array

Default: []

Description: List of files/patterns to load in the browser.

See config/files

pattern

  • All of the relative patterns will get resolved using the basePath first.
  • If the basePath is a relative path, it gets resolved to the directory where the configuration file is located.
  • Eventually, all the patterns will get resolved into files using glob, so you can use minimatch expressions like test/unit/**/*.spec.js.

Type: String

No Default

Description: The pattern to use for matching. This property is mandatory.

watched

Type: Boolean

Default: true

Description: If autoWatch is true all files that have set watched to true will be watched for changes.

included

Type: Boolean

Default: true

Description: Should the files be included in the browser using <script> tag?

served

Type: Boolean

Default: true

Description: Should the files be served by Karma's webserver?

nocache

Type: Boolean

Default: false

Description: Should the files be served from disk on each request by Karma's webserver?

Configuration Options

basePath

Type: String

Default: ''

Description: The root path location that will be used to resolve all relative paths defined in files and exclude. If the basePath configuration is a relative path then it will be resolved to the __dirname of the configuration file.

frameworks

Type: Array

Default: []

Description: List of frameworks you want to use. Like ['openui5', 'qunit', 'phantomjs-shim']

port

Type: Number

Default: 9876

Description: The port where the web server will be listening.

browsers

Type: Array

Default: []

Possible Values:

  • Chrome (launcher comes installed with Karma)
  • PhantomJS (launcher comes installed with Karma)
  • Firefox (launcher requires karma-firefox-launcher plugin)
  • IE (launcher requires karma-ie-launcher plugin)
  • Safari (launcher requires karma-safari-launcher plugin)

Description: A list of browsers to launch and capture. When Karma starts up, it will also start up each browser which is placed within this setting. Once Karma is shut down, it will shut down these browsers as well. You can capture any browser manually by opening the browser and visiting the URL where the Karma web server is listening (by default it is http://localhost:9876/).

singleRun

Type: Boolean

Default: false

Description: Continuous Integration mode.

If true, Karma will start and capture all configured browsers, run tests and then exit with an exit code of 0 or 1 depending on whether all tests passed or any tests failed.

proxies

Type: Object

Default: {}

Description: A map of path-proxy pairs.

Example:

proxies: {
	'/static': 'http://gstatic.com'
	'/web': 'http://localhost:8080'
	'/img/': '/base/test/images/'
}

reporters

Type: Array

Default: ['progress']

Possible Values:

  • dots
  • progress

Description: A list of reporters to use.

Grunt

karma-runner/grunt-karma: Grunt plugin for Karma.

Install

$ npm install karma --save-dev
$ npm install grunt-karma --save-dev

Config

Example

karma: {
  unit: {
    files: [
      { src: ['test/**/*.js'], served: true },
      { src: ['lib/**/*.js'], served: true, included: false }
    ]
  }
}

Gruntfile.js

'use strict';

module.exports = function(grunt) {

	grunt.initConfig({

		dir: {
			webapp: 'webapp',
			test: 'test',
			dist: 'dist',
			bower_components: 'bower_components'
		},

		karma: {
			options: {
				basePath: '',
				frameworks: ['openui5', 'qunit', 'phantomjs-shim'],
				proxies: {
					'/resources/': 'http://localhost:8080/resources/',
					//the following rules are only required when OPA5 tests run the application in an iFrame
					// '/testService.html': "http://localhost:8080/testService.html",
					// '/app/': 'http://localhost:8080/', //this mapping comes from testService.html - see resource mappings there
					// '/test/': 'http://localhost:8080/test-resources/' //this mapping comes from testService.html - see resource mappings there
				},
				openui5: {
					path: 'http://localhost:9876/resources/sap-ui-core.js',
					useMockServer: false
				},
				client: {
					openui5: {
						config: {
							libs: 'sap.m',
							theme: 'sap_bluecrystal',
							compatVersion: 'edge',
							resourceroots: {
								'demo.app': '/base/webapp',
								'test': '/base/test',
								// this mapping is required to map the testService.html when OPA5 tests run the application in an iFrame
								// 'demo.app.testService': '/testService'
							}
						}
					}
				},
				files: [
					{
						pattern: '<%= dir.test %>/karma-qunit.js',
						included: true
					},
					{
						pattern: '<%= dir.webapp %>/**/*',
						included: false
					},
					{
						pattern: '<%= dir.test %>/**/!(allTests.js|AllJourneys.js)',
						included: false
					}
				],
				port: 9876,
				colors: true,
				logLevel: 'INFO',
				browsers: [grunt.option('browsers') || 'PhantomJS']
			},
			unitTests: {
				files: [
					{
						src: ['<%= dir.test %>/unit/allTests.js'],
						included: true
					}
				],
				reporters: ['progress'],
				autoWatch: true,
				singleRun: false
			},
			'unitTests-ci': {
				files: [
					{
						src: ['<%= dir.test %>/unit/allTests.js'],
						included: true
					}
				],
				reporters: ['junit', 'coverage'],
				junitReporter: {
					outputFile: 'reports/TEST-unit.xml'
				},
				preprocessors: {
					'<%= dir.webapp %>/**/*.js': ['coverage']
				},
				coverageReporter: {
					type: 'cobertura',
					dir: 'reports/coverage/unit'
				},
				autoWatch: false,
				singleRun: true
			},
			integrationTests: {
				files: [
					{
						src: ['<%= dir.test %>/integration/AllJourneys.js'],
						included: true
					}
				],
				reporters: ['progress'],
				autoWatch: true,
				singleRun: false
			},
			'integrationTests-ci': {
				files: [
					{
						src: ['<%= dir.test %>/integration/AllJourneys.js'],
						included: true
					}
				],
				reporters: ['junit', 'coverage'],
				junitReporter: {
					outputFile: 'reports/TEST-integration.xml'
				},
				preprocessors: {
					'<%= dir.webapp %>/**/*.js': ['coverage']
				},
				coverageReporter: {
					type: 'cobertura',
					dir: 'reports/coverage/integration'
				},
				autoWatch: false,
				singleRun: true
			}
		}
	});

	// These plugins provide necessary tasks.
	grunt.loadNpmTasks('grunt-openui5');
	grunt.loadNpmTasks('grunt-karma');

	// Test tasks
	grunt.registerTask('unitTests', ['openui5_connect:src', 'karma:unitTests']);
	grunt.registerTask('unitTests-ci', ['openui5_connect:src', 'karma:unitTests-ci']);
	grunt.registerTask('integrationTests', ['openui5_connect:src', 'karma:integrationTests']);
	grunt.registerTask('integrationTests-ci', ['openui5_connect:src', 'karma:integrationTests-ci']);
};