Question

Thank you in advance to everyone that reads through this - there's quite a lot of detail to the question.

I am trying to introduce javascript testing into our project using Jasmine. The tests I've written work in the browser but not using PhantomJS via Resharper.

I'm guessing I'm missing something about the plumbing required to parse our JS files (maybe I'm identifying a problem with our JS setup). Any help hugely appreciated...

I have setup Jasmine tests inside Visual Studio using jasmine...

// The Jasmine Test Framework
/// <reference path="../jquery-1.10.1.js"/>
/// <reference path="lib/jasmine-1.3.1/jasmine.css"/>
/// <reference path="lib/jasmine-1.3.1/jasmine.js"/>
/// <reference path="lib/jasmine-1.3.1/jasmine-html.js"/>
/// <reference path="lib/jasmine-jquery.js"/>
/// <reference path="lib/mock-ajax.js"/>
// Classes to test
/// <reference path="../MyNamespace.Navigation.js"/>

describe("Breadcrumbs", function () {
    it("should have a window object", function() {
        //this test passes in PhantomJS and the browser
        expect(window).toBeDefined();
    });

    it("should have the base object available", function() {
        //this test only passes in the browser
        expect(window.MyNamespace).toBeDefined();
    });
});

this references a JS file which contains...

(function (app, $, undefined) {
     //do things here
}(window.MyNameSpace = window.MyNamespace || {}, jQuery));

I have a SpecRunner.html which can successfully run my tests... it's the specrunner html that comes with Jasmine 1.3.1 standalone with the head edited like so...

 <link rel="stylesheet" type="text/css" href="lib/jasmine-1.3.1/jasmine.css">
  <script type="text/javascript" src="../jquery-1.10.1.js"></script>
  <script type="text/javascript" src="lib/jasmine-1.3.1/jasmine.js"></script>
  <script type="text/javascript" src="lib/jasmine-1.3.1/jasmine-html.js"></script>

  <!-- include source files here... -->
  <script type="text/javascript" src="../MyNamespace.Navigation.js"></script>

  <!-- include spec files here... -->
  <script type="text/javascript" src="spec/BreadcrumbsSpec.js"></script>

These tests are being run either by my SpecRunner.html (which works fine) or through Resharper 7s testrunner using PhantomJS which I'm guessing is not evaluating the code in my JS code file..?


Update: some extra details...

Resharper version is:

JetBrains ReSharper 7.1.3 Full Edition Build 7.1.3000.2254 on 2013-04-10T16:48:18

and Phantom JS version is 1.9.1.0

And I've just realised I don't have the actual error in my question - Doh!

TypeError: 'undefined' is not an object (evaluating 'window.MyNamespace.Whatever') in http://localhost:47815/Tests.js (line 26)
TypeError: 'undefined' is not an object (evaluating 'window.MyNamespace.Whatever')
    at  BreadcrumbsSpec.js: line 26
    at  jasmine.js: line 1035
    at  jasmine.js: line 2053
    at  jasmine.js: line 2006
    at  jasmine.js: line 2335
    at  jasmine.js: line 2053
    at  jasmine.js:2043

Interestingly Tests.js is not my file - I guess that's a R# thing.

Ah - Tests.js is my spec file (i.e. BreadcrumbsSpec.js in this instance). If I launch switch options to tell the Resharper TestRunner to use a browser I get the same results (i.e. only window is defined) and a blank page in the browser...

Was it helpful?

Solution

Eureka... the problem was an ID-ten-T otherwise known as PEBCAK

I hadn't thought about or investigated how R# was actually going to run the tests. And so had set the reference paths in the Spec.js relative to my specrunner.html

R# actually injects the Spec file into an html page as an inline script so my relative paths were all wrong.

I simply set the reference paths as absolute from the project root and all was fine.

// The Jasmine Test Framework
/// <reference path="/Scripts/jquery-1.10.1.js"/>
/// <reference path="/Scripts/tests/lib/jasmine-1.3.1/jasmine.js"/>
/// <reference path="/Scripts/tests/lib/jasmine-1.3.1/jasmine-html.js"/>
/// <reference path="/Scripts/tests/lib/jasmine-jquery.js"/>
/// <reference path="/Scripts/tests/lib/mock-ajax.js"/>
// Classes to test
/// <reference path="/Scripts/MyNamespace.Navigation.js"/>

Thanks to R# people for engaging with me on Twitter!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top