Reputation: 12690
I've been trying to get unit tests working for my node application and I have followed the guide here as well as searched on SO and Google and I am very unclear about what I am doing wrong or if What I am trying to do is even right.
I have created my own module for simplicity say it is called myModule.js
and contains:
exports.mul = function (a, b){
return a * b;
};
I have set up my Karma init to allow requirejs see config file and test-main.js
karma.config.js
:
// Karma configuration
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine', 'requirejs'],
files: [
{pattern: 'lib/**/*.js', included: false},
{pattern: 'src/**/*.js', included: false},
{pattern: 'test/**/*Spec.js', included: false},
'test/test-main.js',
],
exclude: [
'src/main.js'
],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
captureTimeout: 60000,
singleRun: false
});
};
test-main.js
var tests = [];
for (var file in window.__karma__.files) {
if (/Spec\.js$/.test(file)) {
tests.push(file);
}
}
requirejs.config({
// Karma serves files from '/base'
baseUrl: '/base/src',
paths: {
'jquery': '../lib/jquery',
'underscore': '../lib/underscore',
'myModule' : 'myModule',
},
shim: {
'underscore': {
exports: '_'
}
},
// ask Require.js to load these files (all our tests)
deps: tests,
// start test run, once Require.js is done
callback: window.__karma__.start
});
And this is my test file:
define(['app', 'jquery', 'underscore', 'myModule'], function(App, $, _, myModule) {
describe('just checking', function() {
it('test My Module', function (){
expect(myModule.mul(2,2)).toBe(4);
});
});
});
the Problem is I keep getting the error Uncaught ReferenceError: exports is not defined at C:/myProject/src/myModule.js:1
.
Also any time I try to require a node module like mongoose for example I get the error Uncaught Error: Module name "mongoose" has not been loaded yet for context: _. Use require([])
. I have tried to include it the same way I have my own module but with no luck, just get the same error.
Am I doing this completely wrong? What I would really like to be able to do is test my custom modules the same way as I use them ie inside my test file i would do something like the following:
var myModule = require("./myModule");
var value = myModule.mul(3,4)
expect(value).toBe(12)
Obviously using the define in the test would be fine if it worked but I also need to be able to test modules that "require" other modules. And if at all possible I would prefer not to have to manually update the karma config or test-main for every module that is required indirectly ( i.e. if I'm testing module a and it requires modules band c I would expect to update Karma config with information about module a but not b or c or any modules required by b or c)
I have barely used Grunt but if this is a way to solve all this please feel free to point me in that direction or similar.
Any Help, advice or useful links would be greatfully appreciated and if for some reason this is not possible in Karma, is there another framework that I could do this with ( preferably one that uses Jasmine.
Upvotes: 4
Views: 7629
Reputation: 13570
This is CommonJS module definition:
exports.mul = function (a, b){
return a * b;
};
You are using Require.js, so you should use AMD module definition:
define(function (){
return {
mul : function (a, b){
return a * b;
}
};
});
Also, karma
is test runner for browser tests, you don't need it for node tests.
Upvotes: 5