computmaxer / karma-jspm

Other
74 stars 50 forks source link

not working with amd modules #136

Closed cResults closed 8 years ago

cResults commented 8 years ago

"jspm": "^0.16.19", "karma-jspm": "^2.0.1",

Given the karma.conf, test-main-karma.js, and windowTools.js below. The karma.conf loads the test-main-karma.js which adds the amd methods define and require to the window.

Then test-main-karma.js requires jquery, knockout, Q, and windowTools. When it pulls in the windowTools file the define method is undefined.

Could you please advise how to make sure that the amd define and require can be added to window to be available to all test modules? Maybe add an amd setting that when set to true karma-jspm would take care of adding it to the window?

Thank you for contributing very helpful library to the community.

karma.conf:

module.exports = function(config) {
  config.set({
    basePath: 'wwwroot',
    frameworks: ['jspm', 'jasmine-jquery', 'jasmine', 'jasmine-matchers'],
    jspm: {
        loadFiles: [
          'test/test-main-karma.js',
          'src/**/*.test.js',
        ],
        serveFiles: [
          'src/**/*!(.test).js',
          'src/**/*.html'
        ],
        paths: {
          "*": "*",
          'api/*': 'src/api/*',
          'form/*': 'src/form/*',
          'models/*': 'src/models/*',
          'testData/*': 'test/data/*'
        }
    },

    preprocessors: {
        'src/**/*.js': ['babel']
    },
    'babelPreprocessor': {
        options: {
            sourceMap: 'inline',
            modules: 'system',
            moduleIds: false,
            //loose: "all",
            optional: [
              "es7.decorators",
              "es7.classProperties",
              "es7.asyncFunctions",
              "es7.functionBind",
            ]
        }
    },

    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  })
}

test-main-karma:

window.define = System.amdDefine;
window.require = window.requirejs = System.amdRequire;

require([
    'jquery',
    'knockout',
    'q',
    'form/windowTools'
],
function ($, ko, Q) {
    window.$ = $;
    window.ko = ko;
    window.Q = Q;
    if (!ko.isObservableArray) {
        ko.isObservableArray = function (obj) {
            return ko.isObservable(obj) && !(obj.destroyAll === undefined);
        };
    }
});

windowTools:

define(['jquery'],

    function windowTools($) {

        function getParentHash() {
            return window.parent.location.hash;
        }

        function getPathName() {
            return window.location.pathname;
        }

        return {
            GetParentHash: getParentHash,
            GetPathName: getPathName
        };
    });
cResults commented 8 years ago

This was solved by changing the preprocessors glob route to include the files in the test folder.

    preprocessors: {
        'wwwroot/**/*.js': ['babel']
    },