s9tpepper / karma-cucumberjs

A Cucumber.js Karma test runner adapter.
MIT License
54 stars 21 forks source link

test runs fail randomly, unable to execute the karma-loaded function #9

Open travi opened 10 years ago

travi commented 10 years ago

When running my tests, they fairly regularly fail with the following message: TypeError: 'undefined' is not a function (evaluating 'window.__karma__.loaded()'). Rerunning the tests solves the problem, but the instability is frustrating when developing locally and unacceptable when it randomly fails on the ci-server (travis-ci).

Is it possible that I have configured something incorrectly?

I'm executing the tests with grunt and the project is available here, if that would help diagnose.

michal-lipski commented 9 years ago

I have the same error on first run. Tests run with no problems after second try

package.json

{
    "devDependencies": {
        "barcoder": "~1.1.1",
        "grunt": "~0.4.4",
        "jasmine-reporters": "1.0.1",
        "karma": "~0.12.24",
        "karma-chai-plugins": "^0.2.3",
        "karma-chrome-launcher": "~0.1.5",
        "karma-coverage": "^0.2.6",
        "karma-cucumberjs": "krzysztof-jelski/karma-cucumberjs",
        "karma-jasmine": "~0.1.5",
        "karma-junit-reporter": "~0.2.2",
        "karma-ng-html2js-preprocessor": "~0.1.0",
        "karma-phantomjs-launcher": "~0.1.4",
        "protractor": "~0.21.0",
        "request": "~2.34.0"
    }
}

cucumber.conf.js

module.exports = function (config) {
    var cucumberConfig = {
        basePath: '../../../',
        exclude: [],
        files: [
           ..some files...
        ],
        ngHtml2JsPreprocessor: {
            moduleName: 'karma.cached.htmls',
            cacheIdFromPath: function (filepath) {
                return filepath.replace("main/webapp/app/", "/app/");
            }
        },
        preprocessors: {'**/*.html': ['ng-html2js']},
        frameworks: ['cucumberjs', 'chai'],
        browsers: [
            'PhantomJS'
        ],
        port: 9877,
        urlRoot: '/_karma_/',
        singleRun: true,
        autoWatch: false
    };

    config.set(cucumberConfig);
};
amatiasq commented 9 years ago

Same error here, any progress?

amatiasq commented 9 years ago

Found it, it was a HTML parsing - JS timeout race condition. Pull request: https://github.com/s9tpepper/karma-cucumberjs/pull/14

In the meantime if you still have this issue you can use the repository where this bug is already fixed: https://github.com/amatiasq/karma-cucumberjs

jrobison153 commented 8 years ago

I had to close my previous pull request, the race condition was not resolved. After a little more investigation I think the problem is caused by the mix of this adapter which uses requirejs and the Karma load which does not. Within Karma's context.html document the last script tag makes a call to window.karma.loaded(), however this adapter is loading asynchronously at the same time, it's pure chance if it's finished loading before Karma tries to call start().

My previously closed pull request contains the necessary fixes to integrate with the Karma framework, primarily do not monkey with karma.loaded and do not call start() from the adapter. The trick now is to make sure the adapter loads completely before karma attempts to call start(). I see now why s9tpepper was trying to override the loaded function and was calling start() from the adapter code, it was an attempt to work around this race condition.

A solution could be to remove requirejs, I don't see the value in AMD here. Module management could be handled by commonjs and browserify/webpack.

s9tpepper commented 8 years ago

Yea I thought this was resolved and then it reared its ugly head again. Going to try a few things this week to resolve this once and for all.

inthegarage commented 8 years ago

@s9tpepper Did you manage to research the problem? It seems to happen very regularly here. In the meantime I found that the following fix in the context.html got things moving:

replace: window.karma.loaded();

with: function loadKarma() { if (window.karma.loaded) window.karma.loaded(); else { setTimeout(loadKarma, 500); } } loadKarma();

This change in node_modules/karma/static/context.html and debug.html in the same dir.

Ok so no retry count etc, but it will get you moving in the right direction.