Skip to content

Instantly share code, notes, and snippets.

@wendy0402
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wendy0402/68708adc19317b24ba7e to your computer and use it in GitHub Desktop.
Save wendy0402/68708adc19317b24ba7e to your computer and use it in GitHub Desktop.
configuration example for setup test environment using karma, requirejs, mocha, chai, and sinon
// Karma configuration
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: ['mocha', 'requirejs', 'chai-sinon'],
// list of files / patterns to load in the browser
files: [
'test-main.js',
//example
{pattern: 'app/js/lib/**/*.js', included: false},
{pattern: 'app/js/test/**/*.js', included: false},
{pattern: 'app/templates/**/*.html', included: false}
//{pattern: 'location file(s)', included: false}
],
//put all of your pattern in above, but don't delete the test-main.js(for requirejs)
// 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: false,
// 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
});
};
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
var pathToModule = function(path) {
return path.replace(/^\/base\/app\//, '').replace(/\.js$/, '');
};
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
});
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base/app/',
paths: {
'jquery': 'js/lib/jquery/jquery',
'underscore': 'js/lib/underscore/underscore',
'backbone': 'js/lib/backbone/backbone',
'text': 'js/lib/text/text'
},
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment