"Uncaught TypeError: cannot read property" ... "from null" using RequireJS with karma

I am trying to run Jasmine tests in Karma. This is for an AngularJS application using RequireJS, but at the moment the test is a simple test expect(true).toBe(true);without Angular dependencies. I found that it starts when I turn off the Karma module requirejsand comment-out "client/app/require-shared.js"and "test/require-test.js"in filesand load the testSpec.js file directly.

However, as soon as I turn on requirejsand add RequireJS configuration files back to karma.conf.js files- saving the testSpec.js file there “statically” - I see errors

Chrome 33.0.1750 (Windows 7) ERROR
  Uncaught TypeError: Cannot read property 'failedCount' of null
  at C:/.../project/node_modules/karma-jasmine/lib/adapter.js:126
Chrome 33.0.1750 (Windows 7) ERROR
  Uncaught TypeError: Cannot read property 'length' of null
  at C:/.../project/node_modules/karma-jasmine/lib/jasmine.js:2518
Chrome 33.0.1750 (Windows 7): Executed 1 of 2 ERROR (0.009 secs / 0.001 secs)

If I try to run the full Angular test, I get errors with respect to $injectorzero, but I think all this is due to this problem of the requirejs module. What am I missing in my setup to make this work?

Test /karma.conf.js

module.exports = function(config) {

    config.set({
        basePath: "../", // resolves to "project/"

        frameworks: ["jasmine", "requirejs"],

        files: [
            // load the RequireJS config files first
            "client/app/require-shared.js",
            "test/require-test.js",

            // set included to false for files to be loaded via RequireJS
//          {pattern: "bower_components/**/*.js", included: false},
//          {pattern: "client/**/*.js",           included: false},
//          {pattern: "test/unit/*Spec.js",       included: false}
            {pattern: "test/unit/*Spec.js"}
        ],


        // list of files to exclude
        exclude: [
//          "client/app/app.js",
//          "client/app/bootstrap.js",
//          "client/app/require-main.js",
            "*.conf.js"
        ],

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

client / application / require-shared.js

requirejs.config({
    paths: {
        "app":            "../app/app",
        "angular":        "../extlibs/angular-1.2.14",
        "jquery":         "../extlibs/jquery-2.1.0.min",
        "ng-cookies":     "../extlibs/AngularPlugins/angular-cookies-1.2.13",
        "ng-resource":    "../extlibs/AngularPlugins/angular-resource-1.2.13",
        "ng-route":       "../extlibs/AngularPlugins/angular-route-1.2.14",
    }

    ,shim: {
        angular: {
            exports: "angular"
        }
        ,"ng-resource": {
            deps: [ "angular" ],
            exports: "ng-resource"
        },
        ,"ng-cookies":     ["angular"]
        ,"ng-route":       ["angular"]
        ,"ngui-bootstrap": ["angular"]
    }
});

test / require-test.js

var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;

var pathToModule = function(path) {
    return path.replace(/^\/base\//, "").replace(/\.js$/, "");
};

Object.keys(window.__karma__.files).forEach(function(file) {
    if (TEST_REGEXP.test(file)) {
        // Normalize paths to RequireJS module names.
        var normalPath = pathToModule(file);
        console.log("adding " + file + " as " + normalPath + " to allTestFiles array");
        allTestFiles.push(normalPath);
    }
});

requirejs.config({
    baseUrl: "/base/client/components/"

    /*
     * "path" is defined in require-shared.js and shared with
     * require-main. Add paths for the testing files.
     */
    ,paths: {
        "test":          "../../test",
        "bower":         "../../bower_components",
        "angular-mocks": "bower/angular-mocks/angular-mocks"
    }

    ,shim: {
        'angular-mocks': {
            deps: [ 'ng-resource' ],
            exports: 'angular.mock'
        }
    }

    ,deps: allTestFiles
    ,callback: window.__karma__.start
});

test / block / testSpec.js

describe('a basic test of version', function() {
    it('true should be true', function() {
        expect(true).toBe(true);
    });
});
+4
source share
2 answers

The problem was that the TEST_REGEXPgenerated karma initone looked either specin or testin the file name. This led to the require-test.js file being pulled out as a test specification.

Solution is one of

  • rename require-test.js
  • (spec|test) spec RE
  • RE /^(?:(?!require-test\.js$).)*(spec|test)\.js$/i (. )
+2

, (missing package.json): https://github.com/tkellen/js-matchdep/issues/11, , , npm. ( ).

0

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


All Articles