简体   繁体   中英

Karma not finding my jasmine test, TypeError

When trying to run my jasmine test, which is testing my angular customer directive "book directive" using test-runner karma i'm getting the following output errors: (I'm getting various TypeErrors from each script file, not even finding my test let alone showing a failing test))

Karma Output

------ Discover test started ------
Error: TypeError: undefined is not an object (evaluating 'angular.module')
in file:///c:/users/ray/documents/visual%20studio%202013/projects/requirejsdemo/requirejsdemo/scripts/angular-route.js (line 24)
While Running:c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-route.js

Error: TypeError: undefined is not an object (evaluating 'angular.$$minErr')
in file:///c:/users/ray/documents/visual%20studio%202013/projects/requirejsdemo/requirejsdemo/scripts/angular-sanitize.js (line 8)
While Running:c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-sanitize.js

Error: ReferenceError: Can't find variable: define
at global code in file:///c:/users/ray/documents/visual%20studio%202013/projects/requirejsdemo/requirejsdemo/tests/newfeaturetests/directivetest.js (line 3)
While Running:c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\tests\newfeaturetests\directivetest.js

Error: TypeError: Attempted to assign to readonly property.
in file:///c:/users/ray/documents/visual%20studio%202013/projects/requirejsdemo/requirejsdemo/scripts/angular-mocks.js (line 17)
While Running:c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-mocks.js

[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-mocks.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-route.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-sanitize.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-scenario.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\jasmine.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\tests\newfeaturetests\directivetest.js
========== Discover test finished: 0 found (0:00:17.4009953) ==========

karma.conf file

module.exports = function (config) {
config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '../',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'requirejs'],


    // list of files / patterns to load in the browser
    files: [
        "Scripts/angular.js",
           { pattern: "Scripts/require.js", included: false },
              { pattern: "Scripts/jasmine.js", included: false },
                 { pattern: "Scripts/angularAMD.js", included: false },
                    { pattern: "Scripts/jquery-1.8.2.js", included: false },
        { pattern: "Js/**/*.js", included: false },
        { pattern: "tests/NewFeatureTests/*.js", included: false },
        { pattern: "Js/NewFeature/*.html", include: false },

        "tests/test-main.js"
    ],


    // list of files to exclude
    exclude: [
        "Js/main.js"
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        "Js/**.*.html": ["ng-html2js"]
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    plugins: [
        'karma-ng-html2js-preprocessor'
    ],

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
})
}

Jasmine Test

'use strict'

define(['angular', 'jquery',], function (angular) {

    describe('bookDirective', function () {
      var el;

        // Load the myApp module, which contains the directive
        beforeEach(module('testModule'));
        beforeEach(module('Js/NewFeature/Book.html'));

        // Store references to $rootScope and $compile
        // so they are available to all tests in this describe block
        beforeEach(inject(function ($compile, $rootScope) {
            // The injector unwraps the underscores (_) from around the parameter names when matching
            scope = _$rootScope_;

            scope.color = "red";
            scope.title = "ghost";
            //create and compile directive
            el = angular.element("<book title='Ghost' color='red'></book>");

            // compile the dom node and apply the scope, this will return a getter function
            $compile(el)(scope);

            scope.$digest(); // update the html
        }));

        it('background colour of book should be red', function () {
            // check if background color is red
            expect(el.css('background-color').toEqual('red'));
        });

    });

});

How do I get my tests to run?

your base path is basePath: '../', . So, files array should be

files: [
        "../scripts/angular.js",
       "../scripts/angular-route.js",
       "../scripts/angular-mocks.js",
       "../scripts/angular-sanitize.js",
       "../scripts/angular-scenario.js",
       "../scripts/jasmine.js",
       "../tests/newfeaturetests/directivetest.js",
       "../tests/test-main.js"
    ]

Please check base path.

Please also check your index.html file for all script JS that you included and list it in files Array.Otherwise it will lead error if one of those script will dependency for other.

Also in your jasmine test beforeEach(module('Js/NewFeature/Book.html'));

this is wrong.This is not module name registered with angular app.Please check it.remove it.

Also please refer directive testing for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM