I am trying to set my project up to use karma/jasmine. This project also uses knockoutjs (shouldn't really matter) and requirejs (really matters). The project structure is a little different from the norm and that's what is giving me issues. I am running this using karma start
from the command line.
I tried to follow the karma requirejs setup instructions which I feel got me most of the way there. I can set it up to load the test/spec files correctly or the src files correctly but not both. In its current state, I get the following message:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:9876/base/app/test/components/home-pageSpec.js
If I change the baseUrl in test-main.js (generated by karma init
) from '/base/app'
to '/base'
it works just fine until I have a relatively address require.
home.js working (no relative require):
define(['jquery'], function ($) {
function HomeViewModel()
{
this.name = 'myName';
}
return {viewModel: HomeViewModel};
});
home.js not working (relative require):
define(['jquery', 'js/services/someService'], function ($, someService) {
function HomeViewModel()
{
this.name = 'myName';
}
return {viewModel: HomeViewModel};
});
Which gives the following error GET http://localhost:9876/base/js/services/programService.js
. This is because the resource is really at (or should be at) http://localhost:9876/base/app/js/services/programService.js
.
├── app
│ ├── bower_modules ...
│ ├── components
│ │ ├── home
│ │ │ └── home.js
│ ├── index.html
│ ├── js
│ │ ├── lib ...
│ │ ├── services
│ │ │ └── someService.js
│ │ ├── require.config.js
│ │ ├── router.js
│ │ └── startup.js
├── bower.json
├── gulpfile.js
├── karma.conf.js
├── node_modules ...
├── package.json
├── test
│ └── components
│ └── home-pageSpec.js
└── test-main.js
Please tell me how to fix the relative path stuff please.
karma.conf.js:
// Karma configuration
// Generated on Wed Apr 15 2015 10:07:13 GMT-0400 (Eastern Daylight 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
files: [
'test-main.js',
//{pattern: 'app/bower_modules/jquery/dist/jquery.js', included: false},
{pattern: 'app/bower_modules/**/*.js', included: false},
{pattern: 'app/js/lib/**/*.js', included: false},
{pattern: 'test/**/*Spec.js', included: false},
{pattern: 'app/components/**/*.js', included: false},
{pattern: 'app/js/services/**/*.js', included: false}
],
// list of files to exclude
exclude: [
'app/bower_modules/**/tests/*',
'app/bower_modules/**/spec/*',
'app/bower_modules/**/meteor/test.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_DEBUG,
// 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
});
};
test-main.js:
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
var pathToModule = function(path) {
console.log(path + "********");
var p = path.replace(/^\/base\//, '').replace(/\.js$/, '');
console.log(p + "********");
return p;
};
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',
paths: {
"jquery": "app/bower_modules/jquery/dist/jquery",
},
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
home-pageSpec.js:
define(['app/components/home/home'], function(homePage) {
var HomePageViewModel = homePage.viewModel;
describe('Home page view model', function() {
it('should supply a friendly message which changes when acted upon', function() {
expect(1).toEqual(1);
});
});
});