Karma + Browserify + Jasmine + Istanbul + React coverage - reactjs

I'm trying to get the coverage report for my tests but the coverage output for all files is always on a single line showing the require with the path to the file. For example...
The tests however are running fine. This is a react project so I had to include some additional paths to the files and preprocessor to get the tests to run.
I'm not sure if there is something wrong with my karma config? This is what my config currently looks like...
/* global module */
module.exports = function (config) {
'use strict';
config.set({
autoWatch: true,
singleRun: true,
frameworks: ['browserify', 'jasmine'],
files: [
'node_modules/karma-babel-preprocessor/node_modules/babel-core/browser-polyfill.js',
'node_modules/react/react.js',
'src/**/*.jsx',
'src/**/!(*spec).js'
],
browsers: ['PhantomJS'],
preprocessors: {
'node_modules/react/react.js': ['browserify', 'sourcemap'],
'src/**/*.jsx': ['browserify', 'sourcemap', 'coverage'],
'src/**/!(*spec).js': ['browserify', 'sourcemap', 'coverage'],
},
browserify: {
debug: true,
transform: [ 'babelify' ]
},
reporters: ['progress', 'coverage'],
coverageReporter: {
instrumenters: {isparta: require('isparta')},
instrumenter: {
'src/**/*.js': 'isparta',
'src/**/*.jsx': 'isparta'
},
reporters: [
{
type: 'text-summary',
subdir: normalizationBrowserName
},
{
type: 'lcov',
subdir: normalizationBrowserName
},
{
type: 'html',
dir: 'coverage/',
subdir: normalizationBrowserName
}
]
}
});
function normalizationBrowserName(browser) {
return browser.toLowerCase().split(/[ /-]/)[0];
}
};
UPDATE:
I used a commonjs transform as well and was able to get things to work a bit better but tests wouldn't run and the coverage was the transformed code.

Make sure to include all your source code for the coverage. For instance, I have the following karma.conf.js: (check the preprocessor section)
// Karma configuration
// Generated on Mon Sep 07 2015 23:22:13 GMT-0400 (Eastern Daylight Time)
module.exports = function(config) {
var SourceCode = [
'app/app.js',
'app/Modules/*.js', // Basic Path Files (Modules for Source, UnitTests to keep test files separate)
'app/Modules/**/_*.init.js', // Declarative functions needed for next line - Initialization Code
'app/Modules/**/*.js',
'app/UnitTests/**/*.mock.js', // Mock Declarations for Tests
'app/UnitTests/**/*.test.js'
];
var Libraries = [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-animate/angular-animate.js',
'app/bower_components/angular-aria/angular-aria.js',
'app/bower_components/angular-route/angular-route.js',
'app/bower_components/firebase/firebase.js',
...
// Testing
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-material/angular-material-mocks.js',
'app/bower_components/mockfirebase/browser/mockfirebase.js',
'node_modules/sinon/pkg/sinon.js',
];
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai'], //
// list of files / patterns to load in the browser
files: Libraries.concat(SourceCode),
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'app/Modules/**/*.js': ['coverage'] // Ensure all files are in Code Coverage
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'coverage'],
coverageReporter: {
type : 'html',
dir : 'docs/coverage/'
},
logLevel: 'LOG_DEBUG',
// 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: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'], // 'Chrome',
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
})
}

Related

How to import typescript files with karma-systemjs?

Background
I'm trying to set up a test runner for an Angular 1x app, but I'm not able to get Karma to work with Systemjs and Typescript.
The tests are written in Typescript, and we use Systemjs to import files that use ES6 modules. Most files do not use ES6 modules.
Instead of using a karma prepreocessor like karma-typescript, I'm trying to use Systemjs to transpile my Typescript. Is that the wrong way to go? It doesn't seem like any transpiling is happening!
The error
When I run karma start, the script fails on the import of a typescript namspace:
import core = myApp.core;
Chrome 52.0.2743 (Windows 7 0.0.0) ERROR
Error: (SystemJS) SyntaxError: Unexpected token import
Evaluating app/scripts/site/core/components/settingsMenu/settingsMenu.spec.ts
Error loading app/scripts/site/core/components/settingsMenu/settingsMenu.spec.ts
Here is my karma.conf.js file, based on this example.
// Karma configuration
// Generated on Thurs Apr 13 2017
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['systemjs', 'jasmine'],
systemjs: {
configFile: 'config.js',
config: {
paths: {
'systemjs': 'app/vendor/systemjs/system.src.js',
'system-polyfills': 'app/vendor/systemjs/system-polyfills.src.js',
'typescript': '/node_modules/typescript/lib/typescript.js'
},
packages: {
'app': {
defaultExtension: 'ts'
}
},
transpiler: 'typescript'
},
serveFiles: [
'app/scripts/**/*!(spec).ts'
]
},
// list of files / patterns to load in the browser
files: [
{ pattern: 'app/scripts/**/*.spec.ts' }
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
systemjs config:
System.config({
baseURL: './',
defaultJSExtensions: true
});
Try adding the transpiler property in your systemjs config (config.js):
System.config({
baseURL: './',
defaultJSExtensions: true,
transpiler: 'typescript'
});

Jasmine Inject not working

So I have an angular 1.5 app that I compile with Typescript and then bundle it via Browserify.
Now when I want to test it with Karma and Jasmine I get an error as soon as I want to inject a service (it works fine in the browser with strict-di enabled).
Does someone see where the error is?
CacheServiceTest.ts
import {CacheService} from "../../../app/services/CacheService";
import {Application} from "../../../app/Application";
describe("AppComponent", () => {
beforeEach(() => {
angular.mock.module(Application.name);
});
it("should contain CacheService", () => {
inject((CacheService:CacheService) => {
console.log(CacheService);
});
});
});
Karma config
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: "..",
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ["jasmine", "browserify"],
// list of files / patterns to load in the browser
files: [
"node_modules/angular/angular.js",
"node_modules/angular-mocks/angular-mocks.js",
"node_modules/ngLeague/dist/ngLeague.js",
"node_modules/angular-localforage/dist/angular-localForage.js",
"app/**/*.ts",
"tests/unit/**/*.ts",
// JSON fixture
{
pattern: "tests/fixtures/**/*.json",
watched: true,
served: true,
included: false
}
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
"app/**/*.ts": ["browserify", "ng-annotate"],
"tests/unit/**/*.ts": ["browserify", "ng-annotate"],
"dist/ngCache.js": "coverage"
},
// Browserify config
browserify: {
debug: true,
plugin: [
["tsify", {target: "ES5"}]
]
},
// test results reporter to use
// possible values: "dots", "progress"
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ["progress", "coverage"],
coverageReporter: {
type : "html",
dir : "tests/coverage/"
},
// 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: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ["PhantomJS"],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
Error
forEach#/home/kme/Documents/ngCache/node_modules/angular/angular.js:322:24
loadModules#/home/kme/Documents/ngCache/node_modules/angular/angular.js:4548:12
createInjector#/home/kme/Documents/ngCache/node_modules/angular/angular.js:4470:30
workFn#/home/kme/Documents/ngCache/node_modules/angular-mocks/angular-mocks.js:2954:60
inject#/home/kme/Documents/ngCache/node_modules/angular-mocks/angular-mocks.js:2934:46
/tmp/fe42636aeb0f927c8dcd9afa7c80f051.browserify:16447:15 <- tests/unit/services/CacheServiceTest.ts:14:15
/home/kme/Documents/ngCache/node_modules/angular/angular.js:4588:53

Grunt Test Task

i have set up a grunt task for running test using Karma and Jasmine for testing an AngularJS framework!
For some reasons same configurations i have used for other projects fails for this one. here is the error message am getting:
forEach#/home/username/sw-projects/project/bower_components/angular/angular.js:340:24
loadModules#/home/username/sw-projects/project/van/bower_components/angular/angular.js:4419:12
createInjector#/home/username/sw-projects/project//bower_components/angular/angular.js:4344:22
workFn#/home/jofomah/sw-projects/van/bower_components/angular-mocks/angular-mocks.js:2409:60
/home/username/sw-projects/project/bower_components/angular/angular.js:4459:53
Expected undefined to be defined.
/home/username/sw-projects/project/app/common/utility/utility.service.spec.js:16:34
My Karma config:
// Karma configuration
// Generated on Fri Mar 18 2016 14:24:49 GMT+0100 (WAT)
var bowerJS = require('wiredep')({
dependencies: true,
exclude: [/jasmine$/],
devDependencies: true
}).js;
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: bowerJS.concat([
'.tmp/*.js',
// 'config.js',
// 'country-config.js',
'app/routes.js',
'app/app.js',
'app/common/rest/rest-client.js',
'app/common/utility/utility.service.js',
'app/common/settings/settings.js',
'app/common/brand/brand.directive.js',
'app/common/footer/footer.directive.js',
'app/common/week-picker/week-picker.directive.js',
'app/common/state-selector/state-selector.directive.js',
'app/common/loading-blanket/loading-blanket.directive.js',
'app/common/transform-listener/transform-listener.module.js',
'app/common/transform-listener/transform-listener.service.js',
'app/common/transform-listener/transform-listener-client.factory.js',
'app/common/http-error-modal/http-error-modal.js',
'app/modules/dashboards/index.js',
'app/modules/dashboards/state-dashboards.service.js',
'app/modules/dashboards/zone-dashboards.service.js',
'app/modules/data/dashboard/dashboard.controller.js',
'app/modules/data/upload/upload-form.directive.js',
'app/modules/data/inbox/inbox.controller.js',
'app/modules/data/inbox/inbox.service.js',
'app/modules/data/index.js',
'app/modules/users/users.service.js',
'app/modules/users/login-form/login-form.directive.js',
'app/modules/users/user-management-navbar-el/user-management-navbar-el.directive.js',
'app/modules/users/password-reset/password-reset.directive.js',
'app/modules/users/index.js',
'app/modules/settings/indices/index.directive.js',
'app/modules/settings/indices/indicator.directive.js',
'app/modules/settings/indices/indices.directive.js',
'app/modules/settings/indices/index.js',
'app/modules/settings/entities/entities.directive.js',
'app/modules/settings/entities/index.js',
'app/modules/settings/index.js',
'app/modules/analysis/chart.directive.js',
'app/modules/analysis/bar/bar.controller.js',
'app/modules/analysis/matrix/matrix.controller.js',
'app/modules/analysis/stock/stock.controller.js',
'app/modules/analysis/products.constant.js',
'app/modules/analysis/index.js',
'app/modules/entities/typeahead/agencies/agencies.directive.js',
'app/modules/entities/typeahead/districts/districts.directive.js',
'app/modules/entities/typeahead/regions/regions.directive.js',
'app/modules/entities/typeahead/sites/sites.directive.js',
'app/modules/entities/typeahead/zones/zones.directive.js',
'app/modules/entities/typeahead/index.js',
'app/modules/entities/admin/sites.directive.js',
'app/modules/entities/admin/site.directive.js',
'app/modules/entities/admin/zones.directive.js',
'app/modules/entities/admin/zone.directive.js',
'app/modules/entities/admin/regions.directive.js',
'app/modules/entities/admin/region.directive.js',
'app/modules/entities/admin/districts.directive.js',
'app/modules/entities/admin/district.directive.js',
'app/modules/entities/admin/index.js',
'app/modules/entities/entities.service.js',
'app/modules/entities/index.js',
//
//'app/**/*.module.js',
//'app/**/*.js',
//'app/**/*.service.js',
'app/**/*.spec.js',
//'app/**/*.html'
]),
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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_DEBUG,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
plugins: [
'karma-*'
]
})
}
my test is :
'use strict';
/*global module: false, inject: false */
describe('utility', function(){
var utility;
beforeEach(module('eha.van.utility'));
beforeEach(inject(function(_utility_){
utility = _utility_
}));
it('should be defined', function(){
expect(utility).toBeDefined();
});
});

How to test html tags in karma test

I want to test my html tags(text,div,span,etc...)
I am using angularjs,karma with jasmine framework.
This my karma file, here i include ng-html2js preprocess.
// Karma configuration
// Generated on Tue Jan 26 2016 21:38:16 GMT+0530 (India Standard Time)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'../app/js/base.js',
'../global_URL.js',
'../app/**/*.html',
'bower_components/angular-mocks/angular-mocks.js',
'../app/js/app.js',
'test/spec/**/*.js',
'../app/**/*.json'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'../app/**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
moduleName: 'templates'
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
plugins: [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-ng-html2js-preprocessor'
],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
For getting html page i am using httpBackend, my test file like this.
'use strict';
describe('Login Controller', function () {
// load the controller's module
var MainCtrl,
httpBackend,
templateHtml
formElem,
form,
scope;
beforeEach(module('test'));
beforeEach(module('templates'));
beforeEach(module('ngMockE2E'));
//beforeEach(module('sample',['ngMock']));
// Initialize the controller and a mock scope
beforeEach(inject(function ($rootScope, $controller,$compile,$httpBackend,$templateCache) {
scope = $rootScope.$new();
httpBackend = $httpBackend
MainCtrl = $controller('TestController', {
$scope: scope,
});
templateHtml = httpBackend.expectGET('app/admin/login.html').respond([]);
console.log(templateHtml)
formElem = angular.element("<div>test</div>")
$compile(formElem)(scope)
form = scope.form
scope.$apply()
}));
it('should not allow an invalid `width`', function() {
expect(form).toBeDefined();
});
});
Here i print my html page in console, but it's wrote as undefined.
How can i inject my html pages in karma and how can i test html tags.
Please help me, thanks in advance.
to preload html we use the following karma.conf.js ...
'use strict';
module.exports = function (config) {
config.set({
basePath: './',
browsers: ['PhantomJS'],
frameworks: ['jasmine'],
reporters: ['mocha', 'coverage'],
singleRun: true,
preprocessors: {
'src/**/!(*spec)*.js': ['coverage'],
'dest/**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
stripPrefix: 'dest/',
moduleName: 'ngHtmlFiles'
},
coverageReporter: {
type: 'html',
dir: 'coverage'
},
files: [
'dest/vendor.min.js',
'bower_components/angular-mocks/angular-mocks.js',
'src/**/*.js',
'dest/**/*.html'
]
});
};
then in each test ...
beforeEach(module('ngHtmlFiles'));
This will preload the HTML.
However, we do this for a totally different reason to you, namely to prevent httpBackend failing expectations when requests for HTML files are made.
I personally would use protractor E2E testing to test the UI. I believe unit tests should be limited to testing controller / service code only.

Why do I get a 404 message from Karma webserver?

At the moment, I'm trying to setup a demo project with Node/AngularJs/JSPM with ES6 transpiler Babel and Karma.
The serving part is working. I need to improve it later but the first 'hello world' from angular is working.
I'd like to add Karma to run unit tests for the angular app. But I'm getting a 404 warning for jspm_packages, see screenshot below.
My test is not running because it will always fail. My test file looks like this (no angular specific part in there yet):
// HomeController.spec.js
import 'angular-mocks';
describe('Test suite for HomeController', function() {
it('dummy test', function() {
expect(true).toBe(true); // will not fail
});
});
Not sure what's wrong and I've tried many things with-out success maybe I did something wrong. But here is what I've tried in the Karma config:
Tried many different path setting because I think it's an issue with the path
Used proxies
Browserify and Bablify to transpile the sources
JSPM plugin to load the dependencies for testing
You can find the code I'm currently working on here at bitbucket.
The directory structure of my app looks like this:
Here is a screenshot of Karma:
Here is my current Karma config file:
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '.',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jspm', 'jasmine'],
// list of files / patterns to load in the browser
files: [
//'client/test-unit/**/*.test.js'
],
jspm: {
paths: {
// '*': 'client/app/**/*.js'
},
// Edit this to your needs
// config: 'client/app/jspm.config.js',
// Edit this to your needs
loadFiles: ['client/test-unit/**/*.spec.js'],
serveFiles: ['client/app/**/*.js', 'client/app/**/*.css', 'client/app/**/*.html']
},
proxies: {
// '/assets/jspm_packages/': '/client/app/assets/jspm_packages/'
},
plugins:[
'karma-jasmine',
'karma-coverage',
'karma-jspm',
'karma-chrome-launcher'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
// 'client/app/**/*.js': ['browserify'],
// 'client/test-unit/**/*.js': ['browserify']
},
/*browserify: {
debug: true,
transform: [ 'babelify' ]
},*/
// babelPreprocessor: {
// options: {
// sourceMap: 'inline'
// },
// filename: function (file) {
// return file.originalPath.replace(/\.js$/, '.es5.js');
// },
// sourceFileName: function (file) {
// return file.originalPath;
// }
// },
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
I think I've found a fix to my problem.
I had to setup some proxies to make it work.
Also the change of the log level to logLevel: config.LOG_DEBUG for debugging my issue was helpful because then you'll see more details to the problem.
Another problem was that the test file was not transpiled before execution. That was because the preprocessors for transpiling were not setup correctly.
And the packages path inside of the jspm object is required too to make it work.
It's still pretty hard to understand and I'm not sure if there's an easier way to do it.
Anyway the following karma.config is working for me:
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '.',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jspm', 'jasmine'],
// list of files / patterns to load in the browser
files: [
//'client/test-unit/**/*.test.js'
],
proxies: {
'/base/app/': '/base/client/app/',
'/base/assets/jspm_packages/': '/base/client/app/assets/jspm_packages/'
},
jspm: {
// Edit this to your needs
config: 'client/app/jspm.config.js',
// Edit this to your needs
loadFiles: ['client/test-unit/**/*.spec.js'],
serveFiles: ['client/app/**/*.js'],
packages: "client/app/assets/jspm_packages/"
},
plugins:[
'karma-jasmine',
'karma-coverage',
'karma-jspm',
'karma-chrome-launcher'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'test-unit/**/*.js': ['babel', 'coverage']
// 'client/app/**/*.js': ['browserify'],
// 'client/test-unit/**/*.js': ['browserify']
},
/*browserify: {
debug: true,
transform: [ 'babelify' ]
},*/
// babelPreprocessor: {
// options: {
// sourceMap: 'inline'
// },
// filename: function (file) {
// return file.originalPath.replace(/\.js$/, '.es5.js');
// },
// sourceFileName: function (file) {
// return file.originalPath;
// }
// },
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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_DEBUG, //config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
And my test file looks like this:
// HomeController.spec.js
import 'app/app';
import 'angular-mocks';
describe('Test suite for HomeController', function() {
// var homeController;
beforeEach(function() {
module('myApp');
// inject(function($controller) {
// console.log('inject called', $controller);
// //controller = $controller;
// homeController = $controller('homeController');
// });
});
it('should say "Hello World"', inject(function($controller) {
var homeController = $controller('homeController');
// console.log(homeController, angular.mocks);
expect(homeController.hello).toEqual('Hello world from ES6 HomeController.'); // will not fail
})
);
});

Resources