node.js - Why is "exports" not defined in my karma Node unit test and what is preventing requirejs from working as expected -
i've been trying unit tests working node application , have followed guide here searched on , google , unclear doing wrong or if trying right.
i have created own module simplicity called mymodule.js
, contains:
exports.mul = function (a, b){ return * b; };
i have set karma init allow requirejs see config file , 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 = []; (var file in window.__karma__.files) { if (/spec\.js$/.test(file)) { tests.push(file); } } requirejs.config({ // karma serves files '/base' baseurl: '/base/src', paths: { 'jquery': '../lib/jquery', 'underscore': '../lib/underscore', 'mymodule' : 'mymodule', }, shim: { 'underscore': { exports: '_' } }, // ask require.js load these files (all our tests) deps: tests, // start test run, once require.js done callback: window.__karma__.start });
and test file:
define(['app', 'jquery', 'underscore', 'mymodule'], function(app, $, _, mymodule) { describe('just checking', function() { it('test module', function (){ expect(mymodule.mul(2,2)).tobe(4); }); }); });
the problem keep getting error uncaught referenceerror: exports not defined @ c:/myproject/src/mymodule.js:1
.
also time try require node module mongoose example error uncaught error: module name "mongoose" has not been loaded yet context: _. use require([])
. have tried include same way have own module no luck, same error.
am doing wrong? able test custom modules same way use them ie inside test file following:
var mymodule = require("./mymodule"); var value = mymodule.mul(3,4) expect(value).tobe(12)
obviously using define in test fine if worked need able test modules "require" other modules. , if @ possible prefer not have manually update karma config or test-main every module required indirectly ( i.e. if i'm testing module , requires modules band c expect update karma config information module not b or c or modules required b or c)
i have barely used grunt if way solve please feel free point me in direction or similar.
any help, advice or useful links greatfully appreciated , if reason not possible in karma, there framework ( preferably 1 uses jasmine.
this commonjs module definition:
exports.mul = function (a, b){ return * b; };
you using require.js, should use amd module definition:
define(function (){ return { mul : function (a, b){ return * b; } }; });
also, karma
test runner browser tests, don't need node tests.
Comments
Post a Comment