Skip to content

Instantly share code, notes, and snippets.

@chaseconey
Created April 24, 2014 02:40
Show Gist options
  • Save chaseconey/11239634 to your computer and use it in GitHub Desktop.
Save chaseconey/11239634 to your computer and use it in GitHub Desktop.
Karma Setup

Karma Setup Snippets

This is the quick start to get up and running within something other than angular-seed, yeoman, etc. I am actually using these files within a laravel project, so all file paths will be relevant to that framework.

This

  • Runs in Chrome browser
  • Is setup for jenkins (junit reporting)

To run just do karma start in the root of the project

Helpful Resources:

http://www.tuesdaydeveloper.com/2013/06/angularjs-testing-with-karma-and-jasmine/

// ./karma.conf.js
module.exports = function(config) {
config.set({
files : [
'public/bower_components/jquery/dist/jquery.js',
'public/bower_components/angular/angular.js',
'public/bower_components/angular-mocks/angular-mocks.js',
'public/app/**/*.js',
'public/test/unit/**/*.js'
],
autoWatch : true,
reporters: ['progress', 'junit'],
frameworks: ['jasmine'],
browsers : ['Chrome'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine',
'karma-junit-reporter'
],
junitReporter : {
outputFile: 'karma_unit.xml',
}
});
};
{
"name": "karmasetup",
"version": "0.0.0",
"description": "[![Latest Stable Version](https://poser.pugx.org/laravel/framework/version.png)](https://packagist.org/packages/laravel/framework) [![Total Downloads](https://poser.pugx.org/laravel/framework/d/total.png)](https://packagist.org/packages/laravel/framework) [![Build Status](https://travis-ci.org/laravel/framework.png)](https://travis-ci.org/laravel/framework)",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD",
"devDependencies": {
"karma-jasmine": "~0.1.5",
"karma-firefox-launcher": "~0.1.3",
"karma-chrome-launcher": "~0.1.3",
"karma-junit-reporter": "~0.2.2"
},
"dependencies": {
"karma": "~0.12.10",
"bower": "^1.3.1",
}
}
// ./public/app/test/unit/test.js
'use strict';
describe('Test Suite', function() {
var scope;//we'll use this scope in our tests
//mock Application to allow us to inject our own dependencies
beforeEach(angular.mock.module('testApp'));
//mock the controller for the same reason and include $rootScope and $controller
beforeEach(angular.mock.inject(function($rootScope, $controller) {
//create an empty scope
scope = $rootScope.$new();
//declare the controller and inject our empty scope
$controller('Ctrl', {$scope: scope});
}));
it('it works!', function () {
expect(scope).toBeDefined();
expect(scope.cards.length).toBe(0);
});
it('works again!!', function () {
expect(scope.rowsPerPage).toBe(5);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment