Why "export" is not defined in my karma Node unit test, and what prevents the requirement to work properly

I am trying to get unit tests that work for my node application, and I followed the manual here , and also searched on SO and Google, and I really don’t understand what I am doing wrong, or that what I am trying to do is even correct.

I created my own module for simplicity and said that it is called myModule.jsand contains:

exports.mul = function (a, b){
    return a * b;
};

I installed Karma initialization to allow requirejs to see the configuration 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 that I keep getting the error Uncaught ReferenceError: exports is not defined at C:/myProject/src/myModule.js:1.

, node, mongoose, Uncaught Error: Module name "mongoose" has not been loaded yet for context: _. Use require([]). , , , .

? , , , , .. :

var myModule = require("./myModule");
var value = myModule.mul(3,4)
expect(value).toBe(12)

, , , , "" . , , (.. a, c, Karma a, b c, , b c)

Grunt, , , .

, , - , , ( , Jasmine.

+4
1

CommonJS:

exports.mul = function (a, b){
    return a * b;
};

Require.js, AMD:

define(function (){
    return {
        mul : function (a, b){
            return a * b;
        }
    };
});

, karma runner , node.

+5

Source: https://habr.com/ru/post/1524720/


All Articles