Showing results for 
Search instead for 
Did you mean: 

Using Karma on Magento 2.x to run unit tests

Using Karma on Magento 2.x to run unit tests

I am trying to setup karma to run the unit tests for our magento site and having a lot of problems.

Here is my karma config file:


// Karma configuration
// Generated on Thu Jan 05 2017 01:04:20 GMT+0000 (UTC)
module.exports = function(config) {
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',

// frameworks to use
// available frameworks:
frameworks: ['jasmine', 'requirejs'],

// list of files / patterns to load in the browser
files: [
{pattern: '../jasmine/tests/**/*.js', included: false},
{pattern: '../../../../pub/static/_requirejs/frontend/Magento/blank/en_US/**/*.js', included: false},
{pattern: '../../../../pub/static/frontend/Magento/blank/en_US/**/*.js', included: true}
//{pattern: '../../../../pub/static/frontend/Magento/blank/en_US/js/bundle/**/*.js', included: true}

// list of files to exclude
exclude: [

// preprocess matching files before serving them to the browser
// available preprocessors:
preprocessors: {
'*.js': 'coverage'

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters:
reporters: ['progress','coverage'],
coverageReporter: {
// specify a common output directory
dir: 'coverage',
reporters: [
// reporters not supporting the `file` property
{ type: 'html', subdir: 'report-html' },
{ type: 'lcov', subdir: 'report-lcov' },
// reporters supporting the `file` property, use `subdir` to directly
// output them in the `dir` directory
{ type: 'cobertura', subdir: '.', file: 'cobertura.txt' },
{ type: 'lcovonly', subdir: '.', file: 'report-lcovonly.txt' },
{ type: 'teamcity', subdir: '.', file: 'teamcity.txt' },
{ type: 'text', subdir: '.', file: 'text.txt' },
{ type: 'text-summary', subdir: '.', file: 'text-summary.txt' },
// web server port
port: 9876,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,

// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,

// start these browsers
// available browser launchers:
browsers: ['PhantomJS'],

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity



Right now I am getting a lot of requirejs errors saying "Error: Mismatched anonymous define() module: {0}" I have been searching all over the place trying to figure out how to set this up and haven't been able to fix it. The reason I am trying to set this up is to generate an LCOV report that I can import into SonarQube to show our test coverage.


Tags (3)

Re: Using Karma on Magento 2.x to run unit tests

Hi @srarnold,


Since this has been posted several days ago, I would like to know if you were able to find the solution.

Please let us know.
Best regards.

If my answer was useful, please give "Kudos" to this post.
If my answer helps you to resolve the problem, click on "Accept as Solution".