mercredi 28 janvier 2015

karma and requirejs with angular

I having troubles to setting the above working together.

I want to implement karma testing in my project, the problem is that the require modules are not defined in the test.


Here is my karma.config.js:



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: http://ift.tt/1ft83uu
frameworks: ['jasmine', "requirejs"],


// list of files / patterns to load in the browser
files: [
{pattern: 'app/scripts/**/*.js', included: false, served: true},
{pattern: 'test/spec/**/*.js', included: false, served: true},
"test/main-test.js"
],




// list of files to exclude
exclude: [
'app/require-main.js'
],


// preprocess matching files before serving them to the browser
// available preprocessors: http://ift.tt/1gyw6MG
preprocessors: {
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: http://ift.tt/1ft83KQ
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: false,


// start these browsers
// available browser launchers: http://ift.tt/1ft83KU
browsers: ['PhantomJS'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true

});
};


Here is my main-test.js:



var tests = [];
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (/Spec\.js$/.test(file)) {
tests.push(file);
}
}
}
requirejs.config({
// Karma serves files from '/base'
baseUrl: '',

paths: {
"requireMain": "app/scripts/require-main"
},

shim: {
},

// ask Require.js to load these files (all our tests)
deps: tests,

// start test run, once Require.js is done
callback: window.__karma__.start
});


Here is my require-main.js (shorten):



require.config({
paths: {
//bootstrap
app: "app",
...


//3rd party
"angular_route": "../3rd-party/angular-route/angular-route",

//providers


//services
...
normalizer: "../ext/conf/normalizer",
...


//directives
"si-daterangepicker": "ng-directives/si-daterangepicker",
},
shim: {
"angular" : {
deps: ["jquery"],
"exports" : "angular"
}
.....

});


and I have this normalizerSpec.js test file:



define(['app', 'normalizer'], function(app) {
console.log("bew");
describe("service: normalizer", function () {
var normalizer;

beforeEach(module("app"));

beforeEach(inject(function (_normalizer_) {
normalizer = _normalizer_;
}));

var params = {};
var metadata = {};
var data = {};

var response = normalizer.topLanguagesHybrid(metadata, data, params);

var type = typeof response;
expect(type).toEqual("object");
});
});


The error that I am getting is: enter image description here


Note: My project is working with requirejs, how can I make karma see all its dependencies as they are set in require-main.js?


Thanks!


Aucun commentaire:

Enregistrer un commentaire