Error while testing route Karma in e2e tests

11,869

Solution 1

Add this to your e2e config file:

urlRoot = '/_karma_/';

Solution 2

So I was using this exact config ... then I realized that jasmine has no concept of Browser. As soon as I removed jasmine from the frameworks, this error went away.

Of course, I got another one, so off to debug that :)

Solution 3

For anyone else struggling with this: If you are using requirejs and bootstrap the page manually, make sure you still have the ng-app directive in your HTML file. This fixed it for me.

karma 0.9.5 requirejs 2.1.6 angularjs 1.0.7 karma-ng-scenario 0.0.2 karma-chrome-launcher 0.0.2

Also, this is my config for my e2e tests with the web server running on localhost:8000:

module.exports = function(config) {
    config.set({
        frameworks: ['ng-scenario'],

        files: [
            'test/e2e/**/*.js'
        ],

        basePath: '../',

        autoWatch: true,

        proxies: {
            '/': 'http://localhost:8000/'
        },

        urlRoot: '__karma__',

        browsers: ['Chrome'],

        reporters: ['dots'],

        plugins: [
            'karma-ng-scenario',
            'karma-chrome-launcher'
        ]
    });
};

Edit: I played around a bit more with my now working configuration to reproduce the error. It occurred again when I did not sleep() after navigating to the app root. So this might be worth checking, too.

Share:
11,869
koleror
Author by

koleror

Updated on June 07, 2022

Comments

  • koleror
    koleror almost 2 years

    I'm trying to make some basic end to end tests (e2e) on a django/angularjs application using Karma and I'm getting a weird error.

    Here is my test code:

    # testacular-e2e.conf.js
    
    describe('Log in tests', function() {
       it('should show the disconnected home', function() {
          browser().navigateTo('/#');
          expect(browser().location().url()).toBe('/#');
       });
    });
    

    When I'm running this code, I get the following output:
    [DEPRECATED ERROR - left for reference only]
    $>karma start testacular-e2e.conf.js [2013-04-30 22:19:26.465] [WARN] config - "/" is proxied, you should probably change urlRoot to avoid conflicts [2013-04-30 22:19:26.467] [DEBUG] config - autoWatch set to false, because of singleRun INFO [karma]: Karma server started at http://local.host:9876/ INFO [launcher]: Starting browser PhantomJS INFO [PhantomJS 1.9 (Mac)]: Connected on socket id cwhp0lnTraNa4ToQ0HkS PhantomJS 1.9 (Mac) Log in tests should show the disconnected home FAILED TypeError: 'undefined' is not a function (evaluating '$document.injector()') PhantomJS 1.9 (Mac): Executed 1 of 1 (1 FAILED) (0.098 secs / 0.04 secs)

    Any one can help please?
    Thanks in advance :)

    EDIT: Here is my Karma configuration file:

    // Karma configuration
    
    // base path, that will be used to resolve files and exclude
    basePath = '';
    
    // frameworks to use
    frameworks = ['jasmine', 'ng-scenario'];
    
    // list of files / patterns to load in the browser
    files = [
      'app/components/angular/angular.js',
      'app/components/angular-complete/angular-resource.js',
      'app/components/angular-complete/angular-cookies.js',
      'app/components/angular-mocks/angular-mocks.js',
      'app/components/ng-translate/translate.js',
      'app/scripts/*.js',
      'app/scripts/services/services.js',
      'app/scripts/**/*.js',
      'test/spec/**/*.js'
    ];
    
    // list of files to exclude
    exclude = [];
    
    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters = ['progress'];
    
    // web server port
    port = 9876;
    
    // cli runner port
    runnerPort = 9100;
    
    
    // enable / disable colors in the output (reporters and logs)
    colors = true;
    
    
    // level of logging
    // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
    logLevel = LOG_DEBUG;
    
    
    // enable / disable watching file and executing tests whenever any file changes
    autoWatch = true;
    
    
    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    browsers = ['PhantomJS'];
    
    // If browser does not capture in given timeout [ms], kill it
    captureTimeout = 60000;
    
    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun = false;
    
    // plugins to load
    plugins = [
      'karma-jasmine',
      'karma-phantomjs-launcher',
      'karma-chrome-launcher',
      'karma-ng-scenario',
    ];
    

    EDIT 2: Add component.json file

    # component.json
      {
        "name": "myapp",
        "version": "0.0.0",
        "dependencies": {
        "angular": "~1.0.5",
        "json3": "~3.2.4",
        "es5-shim": "~2.0.5",
        "angular-resource": "~1.0.5",
        "angular-cookies": "~1.0.5",
        "angular-complete" : "*",
        "jquery" : "~1.9",
        "bootstrap" : "~2.3.1",
        "bootstrap-js" : "*",
        "jquery.localScroll" : "",
        "jquery.scrollTo" : "1.4.4",
        "ng-translate" : ""
      },
      "devDependencies": {
        "angular-mocks": "~1.0.5",
        "angular-scenario": "~1.0.5"
      }
    }
    

    EDIT 3 : Updated the above Karma conf file + this is my e2e conf file:

    // e2e karma configuration
    
    var fs = require('fs');
    
    // Load from basic karma configuration
    eval(fs.readFileSync('karma.conf.js')+'');
    
    // list of files / patterns to load in the browser
    files = [
      'app/components/angular/angular.js',
      'app/components/angular-complete/angular-resource.js',
      'app/components/angular-complete/angular-cookies.js',
      'app/components/angular-mocks/angular-mocks.js',
      'app/components/ng-translate/translate.js',
      'app/scripts/*.js',
      'app/scripts/services/services.js',
      'app/scripts/**/*.js',
      'test/e2e/**/*.js'
    ];
    
    
    // Proxy the root path to the location of app server
    proxies = {
      '/': 'http://localhost:8000/'
    };
    

    If you need the details of the files imported in the "files" params, let me know. With he actual configuration, the "normal" tests (not the e2e ones) works fine but the e2e tests give me the following error:

    $>karma start karma-e2e.conf.js
    [2013-05-01 11:32:19.047] [WARN] config - "/" is proxied, you should probably change urlRoot to avoid conflicts
    INFO [karma]: Karma v0.9.2 server started at http://localhost:9876/
    INFO [launcher]: Starting browser PhantomJS
    INFO [PhantomJS 1.9 (Mac)]: Connected on socket id HmPYnc9PJUdaGb35pl-K
    PhantomJS 1.9 (Mac) Log in tests should show the disconnected home FAILED
    ReferenceError: Can't find variable: browser
        at /Users/hugo/Projs/wc/angular/test/e2e/scenarios.js:9
        at /Users/hugo/Projs/wc/angular/node_modules/karma-jasmine/lib/adapter.js:107
        at http://localhost:9876/karma.js:111
        at http://localhost:9876/context.html:68
    PhantomJS 1.9 (Mac): Executed 1 of 1 (1 FAILED) (0.082 secs / 0.006 secs)
    
  • koleror
    koleror about 11 years
    Thanks for your answer. I'm using Karma version: 0.8.5 Thanks for your link too. I'll have a look now.
  • koleror
    koleror about 11 years
    What config change will I have to make? Do you have any link talking about that?
  • koleror
    koleror about 11 years
    I just read your link and tried to update to canary but I have an error saying that testacular is deprecated and that I need to use karma instead... Any suggestion?
  • S McCrohan
    S McCrohan about 11 years
    npm install -g karma@canary should get you the current version of what was being talked about in that link; the name change from testacular to karma invalidated a lot of old comments. For notes on some of the config changes, see v0.9.0 in the changelog: github.com/karma-runner/karma/blob/master/CHANGELOG.md
  • koleror
    koleror about 11 years
    Ok after regenerating a clean conf file, it's starting again. However, I get the following error: ReferenceError: Can't find variable: browser when I run the line browser().navigateTo('/#'); Any idea?
  • S McCrohan
    S McCrohan about 11 years
    Take a look at karma's own basic e2e on Git: github.com/karma-runner/karma/tree/master/test/e2e/… - look in the karma.conf.js in particular for things that might be missing from yours. That's my first thought, anyway.
  • koleror
    koleror about 11 years
    I just had a look and added the missing stuff but it's still not working... The only main difference compared to your link is that on github, the configurations are now in a module (this has been changed a few days ago). I tried this but I had an error saying that module is undefined...
  • S McCrohan
    S McCrohan about 11 years
    Hmm. Update the question with your config/etc as they are now, so we can give them a try?
  • koleror
    koleror about 11 years
    Thanks for your help guys. I really appreciate. I just updated my ticket (EDIT 3)
  • koleror
    koleror about 11 years
    Anyone has an idea? I really need to get this fixed... Thanks.
  • habanoz
    habanoz almost 11 years
    This solved the problem for me. If you don't set this when using a proxy for "/" then the karma runner is unable to find the resources it needs to run. Setting urlRoot to a unique URL that's not served by the server under test solves this.
  • hunterloftis
    hunterloftis almost 11 years
    I had the exact same problem... ended up copying and pasting the exact files from that repo in and slowly modifying them to work with my project. Still not sure what broke it. Karma has terrible, terrible docs.
  • isimmons
    isimmons over 10 years
    I tried this and get several "404 Not Found: /__karma__/app/index.html" from the server and both karma and chrome crash.