Skip to content

Instantly share code, notes, and snippets.

@theladyjaye
Created October 13, 2013 18:53
Show Gist options
  • Save theladyjaye/6965926 to your computer and use it in GitHub Desktop.
Save theladyjaye/6965926 to your computer and use it in GitHub Desktop.
Karma with Jasmine and Require (and jasmine-jquery)
define(function(require, exports, module) {
// Imports
// make require load jquery and jasmine-jquery
require('jquery');
require('jasmine/jasmine-jquery');
// jquery-jasmine
// now getFixtures() will be available.
jasmine.getFixtures().fixturesPath = path + 'tests/fixtures';
}); // eof define
// Karma configuration
// Generated on Fri Oct 11 2013 11:32:27 GMT-0700 (PDT)
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',
// frameworks to use
frameworks: ['jasmine', 'requirejs'],
// list of files / patterns to load in the browser
files: [
// using jasmine-jquery, we would like to serve our fixtures:
// note that included is set to FALSE on all of these. I do want
// some of the files accessible though the karma web server though
// so require or jasmine-jquery can load them. They denoted with served:true
// my fixtures
{pattern: 'tests/fixtures/*.html', included: false, served: true},
// my tests
{pattern: 'tests/spec/auf*.js', included: false},
// vendor files: jquery, jasmine-jquery, backbone, marionette, etc....
{pattern: 'tests/lib/**/*.js', included: false, served: true},
// my lib that I will be testing.
{pattern: 'mylib/**/*.js', included: false, served: true},
// this file will be included in the karma bootstrap html file with a <script> tag
'tests/karma-main.js'
],
// don't be fancy with html files:
// using jasmine-jquery, we do not want karma messing with our html files.
// https://github.com/karma-runner/karma/issues/740
preprocessors: {},
// list of files to exclude
exclude: [
'**/karma.conf.js'
],
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
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, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
//browsers: ['Chrome', 'Firefox', 'Safari'],
browsers: ['Chrome'],
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
var tests = [];
// we want require to load our test(spec) files
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
// simple pattern that matches our files
// note that these files are available here
// because of our settings in the karma.conf.js files[]
if (/auf.+\.js$/.test(file)) {
tests.push(file);
}
}
}
var path = '';
if (typeof window.__karma__ !== 'undefined') {
path += '/base/';
}
requirejs.config({
// Karma serves files from '/base'
baseUrl: '/base',
paths: {
'lib': 'tests/lib',
'vendor': 'tests/lib/vendor',
'jasmine': 'tests/lib/jasmine',
'auf': 'agency-ui-foundation'
},
map: {
'*': {
'underscore': 'vendor/underscore',
'jquery' : 'vendor/jquery',
'backbone' : 'vendor/backbone',
'marionette': 'vendor/marionette',
'stickit' : 'vendor/stickit',
'handlebars': 'vendor/handlebars'
}
},
shim: {
'jasmine/jasmine-jquery': {
// jasmine is loaded though the karma-jasmine plugin specified in frameworks[] in the karma.conf.js
//'deps': ['jasmine/jasmine']
},
// Vendor shims
'vendor/underscore': {
'exports': '_'
},
'vendor/jquery': {
'exports': '$'
},
'vendor/backbone': {
'deps': ['jquery', 'underscore'],
'exports': 'Backbone'
},
'vendor/marionette': {
'deps': ['jquery', 'underscore', 'backbone'],
'exports': 'Marionette'
},
'vendor/stickit': {
'deps' : ['backbone'],
'exports' : 'Stickit'
}
},
// ask Require.js to load these files (all our tests)
deps: tests,
// start test run, once Require.js is done
// the original callback here was just:
// callback: window.__karma__.start
// I was running into issues with jasmine-jquery though
// specifically specifying where my fixtures were located
// this solved it for me.
callback: function(){
require(['jasmine/karma'], function(){
window.__karma__.start();
});
}
});
@jagin
Copy link

jagin commented Aug 13, 2014

Thanks, that helps me a lot with my configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment