Skip to content

Instantly share code, notes, and snippets.

@anson-vandoren
Created July 23, 2015 10:35
Show Gist options
  • Save anson-vandoren/583eeaa0fc2629492fb1 to your computer and use it in GitHub Desktop.
Save anson-vandoren/583eeaa0fc2629492fb1 to your computer and use it in GitHub Desktop.
Configuration to finally get Karma working with RequireJS. After Karma is installed, run `karma start karma.config.js`
// Karma configuration
// Generated on Wed Jul 22 2015 18:41:14 GMT+0900 (Tokyo 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', 'requirejs'],
// list of files / patterns to load in the browser
// test-main.js configures paths for RequireJS
// All tests are matched by second pattern
// All libraries and game code are matched by third
files: [
'test-main.js',
{pattern: 'src/**/test/javascript/*Spec.js', included: false},
{pattern: 'src/main/webapp/**/*.js', included: false}
],
// list of files to exclude
// Exclude our main.js so RequireJS doesn't try to start with the production configuration
exclude: [
'src/main/webapp/app/main.js'
],
// 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
})
}
// Replacement main.js for Karma running with RequireJS
// Finally figured out after hours and hours of searching due to my non-standard (?)
// directory structure for this project. Modified from the default Karma setup
/* Directory structure like this:
base
|--karma.config.js
|--test-main.js
|--src
\--main
`--webapp
|--app
| |--models
| | `--gameModel.js
| |--main.js
| |--game.js
| |--game.html
| |--*.html
| `--*other*.js
`--lib
|--accounting
| `--accounting.js
|--bootstrap
| `--bootstrap.js
`--require
`--require.js
|--test
`--javascript
`-gameSpec.js
*/
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
// If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
// then do not normalize the paths
var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
allTestFiles.push(normalizedTestModule);
}
});
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base',
// Need to add paths here since otherwise Karma searches for them in /base
// Can't put baseUrl to be src/main/webapp/lib or else it can't find the tests
paths: {
'text': 'src/main/webapp/lib/require/text',
'durandal': 'src/main/webapp/lib/durandal/js',
'plugins': 'src/main/webapp/lib/durandal/js/plugins',
'transitions': 'src/main/webapp/lib/durandal/js/transitions',
'knockout': 'src/main/webapp/lib/knockout/knockout-3.1.0',
'koMapping': 'src/main/webapp/lib/knockout/knockout.mapping-2.4.1.min',
'jquery': ['src/main/webapp/lib/jquery/jquery-1.9.1'],
'accounting': 'src/main/webapp/lib/accounting/accounting.min',
'gameData': 'src/main/webapp/app/gameData.min',
'models/gameModels': 'src/main/webapp/app/models/gameModels'
},
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
@anson-vandoren
Copy link
Author

@master-elodin for whenever you want to get Karma set up

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