Keith Jackson
Keith Jackson

Reputation: 3259

Jasmine not starting in my browser (with Backbone and RequireJS) - have to keep refreshing

I've got just over 400 tests in my Jasmine test suite now but I seem to have a small problem. The specrunner only seems to actually run one in 5 times and it's success seems quite random. When it 'takes' it will all work fine. If it doesn't 'take' then the page just sits there with no errors or anything, it just spins for a while then presents an empty screen.

I'm seeing the same behavior in both Firefox and Chrome.

Has anyone else hit a similar problem and could maybe indicate what the issue could be.

The issue seems to be getting slowly worse, so I'm wondering if it's related to page size. My SpecRunner page is a RAZOR script and looks like this...

@using SiansPlan.Authentication
@using SiansPlan.Spa
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Jasmine Spec Runner for SiansPlan SPA</title>

    <link rel="stylesheet" type="text/css" href="Content/jasmine.css">

    <script type="text/javascript" src="scripts/lib/jasmine.js"></script>
    <script type="text/javascript" src="scripts/lib/jasmine-html.js"></script>
    <script type="text/javascript" src="scripts/lib/jasmine-jquery.js"></script>
    <script src="~/scripts/lib/handlebars.js"></script>
    <script data-main="scripts/test/main.js" src="scripts/lib/require.js"></script>

    <script type="text/javascript">
        require(["../lib/domReady"
            @foreach (var file in Html.PathContentFromRoot("scripts/test/spec", Server))
            {
                <text>,"spec/@file"</text>
            }
            ], function () {
            var jasmineEnv = jasmine.getEnv();
            jasmineEnv.updateInterval = 1000;

            var htmlReporter = new jasmine.HtmlReporter();

            jasmineEnv.addReporter(htmlReporter);

            jasmineEnv.specFilter = function (spec) {
                return htmlReporter.specFilter(spec);
            };

            var currentWindowOnload = window.onload;

            window.onload = function () {
                if (currentWindowOnload) {
                    currentWindowOnload();
                }
                execJasmine();
            };

            function execJasmine() {
                jasmineEnv.execute();
            }

        })();
  </script>

</head>

<body>
    <div style="display:none;">
        @* Key platform variables. *@
        <span id="requestSource">website</span>
        <span id="apiKey">@ApiKeyManager.Instance.GetKey("website")</span>
    </div>
</body>
</html>

If there's anything else I can provide that may help, please just let me know.

UPDATE: This has totally stalled now - I've tracked it down to the window.onload event not firing.

Upvotes: 1

Views: 679

Answers (1)

Keith Jackson
Keith Jackson

Reputation: 3259

This is a hacky answer, but I can't seem to find a better way.

I THINK window onload is firing before it's been properly wired up (probably due to the volume of linked scripts in the preceding JS calls)

In order to hackily fix this - I changed the JavaScript as follows...

<script type="text/javascript">
    require(["../lib/domReady"
        @foreach (var file in Html.PathContentFromRoot("scripts/test/spec", Server))
        {
            <text>,"spec/@file"</text>
        }
        ], function () {
        var jasmineEnv = jasmine.getEnv();
        jasmineEnv.updateInterval = 1000;

        var htmlReporter = new jasmine.HtmlReporter();

        jasmineEnv.addReporter(htmlReporter);

        jasmineEnv.specFilter = function (spec) {
            return htmlReporter.specFilter(spec);
        };

        //var currentWindowOnload = window.onload;

        //window.onload = function () {
        //    if (currentWindowOnload) {
        //        currentWindowOnload();
        //    }

        //    execJasmine();
        //};

        //function execJasmine() {
        //    jasmineEnv.execute();
            //}

        setTimeout(function() {
            jasmineEnv.execute();
        }, 3000);

    })();
  </script>

It's not pretty, but it works - every time

Upvotes: 1

Related Questions