Can Blanket.js work with Jasmine tests if the tests themselves are loaded with RequireJS?

旧城冷巷雨未停 提交于 2019-12-04 20:30:24

I have gotten this working by requiring blanket-jasmine then setting the options

require.config({
    paths: {
        'jasmine': '...',
        'jasmine-html': '...',
        'blanket-jasmine': '...',
    },
    shim: {
        'jasmine': {
            exports: 'jasmine'
        },
        'jasmine-html': {
            exports: 'jasmine',
            deps: ['jasmine']
        },
        'blanket-jasmine': {
            exports: 'blanket',
            deps: ['jasmine']
        }
    }
});

require([
    'blanket-jasmine',
    'jasmine-html',
], function (blanket, jasmine) {
    blanket.options('filter', '...'); // data-cover-only
    blanket.options('branchTracking', true); // one of the data-cover-flags

    require(['myspec'], function() {
        var jasmineEnv = jasmine.getEnv();
        jasmineEnv.updateInterval = 250;
        var htmlReporter = new jasmine.HtmlReporter();

        jasmineEnv.addReporter(htmlReporter);
        jasmineEnv.specFilter = function (spec) {
            return htmlReporter.specFilter(spec);
        };
        jasmineEnv.addReporter(new jasmine.BlanketReporter());
        jasmineEnv.currentRunner().execute();
    });
});

The key lines are the addition of the BlanketReporter and the currentRunner execute. Blanket jasmine adapter overrides jasmine.execute with a no-op that just logs a line, because it needs to halt the execution until it is ready to begin after it has instrumented the code.

Typically the BlanketReport and currentRunner execute would be done by the blanket jasmine adapter but if you load blanket-jasmine itself in require, the event for starting blanket test runner will not get fired as subscribes to the window.load event (which by the point blanket-jasmine is loaded has already fired) therefore we need to add the report and execute the "currentRunner" as it would usually execute itself.

This should probably be raised as a bug, but for now this workaround works well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!