thoughts on coding

April 23, 2012

Testing WWA apps with Jasmine on Windows 8

Filed under: TDD, WWA — Tags: , , , , , , — Frantisek @ 9:51 pm

I work on a project which is WWA app and I must admit that:

  •  the code can quickly ends up in the mess,
  • the language itself is dynamic so there are no compile time errors (just runtime errors)

One of the ways how to ensure higher code quality is unit testing.

I paste here 2 vey important notes about unit testing from this blog because many people argue that they don’t have time to write unit tests OR it’s to advanced approach for them.

Testing is an advanced practice for the pros, not for me

My position is that whatever way you think of your process for writing code, you are testing it, e.g. by refreshing your browser(s) to verify that it is doing what it should. You are simply opting out of automating and improving your testing process, and in the long run (or even not-so-long run) you are spending more time hammering your browser’s refresh button than I spend writing tests that I can run today, tomorrow and next year should I so please.

Testing takes too much time, I’d rather just write production code

Both manual and automated testing takes time. But please, don’t spend an hour or two “evaluating” unit testing and/or TDD and then decide it is a waste of time. Unit testing and TDD are disciplines that need to be practiced just like any other. There is no way to get good at automated testing in a few hours. You need practice, and once you get there, then you will recognize the benefits I’m describing here, and then you will realize how much of a waste manual ad-hoc testing is. Besides, even if writing unit tests and testing your code rigorously takes a little more time, what would you prefer? To fail really fast, or to succeed?

Current situation

Unfortunatelly, there is no project template for unit testings of WWA apps in Visual Studio 2011. There are different projects templates for authoring WWA apps with VS 2011 but NO project template for unit testing and it’s a shame. We, inside Microsoft have the ways how to test WWA apps but they are not public.

The best would be to have a publicly available way how to test WWA apps with community behind.

Solution

First of all, there are some unit testing plugins into VS IDE like JustCode from Telerik or Unit testing plugin from JetBrains but no for running them as Metro WWA application.  So I had to invent my own solution. I had to port Jasmine runner including my own extensions in order to run the unit tests for WWA apps on Windows 8.

I use Jasmine as unit testing framework. I wrote a shell page (or so called test runner shell) which is set as the start page. The shell page runs the whole process which consist of:

  1. configuring Jasmine,
  2. discovering tests and
  3. running the tests upon user input.

In order to fit with Jasmine I had to write also a Jasmine reporter which communicates with the shell and reports the progress of tests execution.

Enought theory, example please.

I ported a referral example from Jasmine project and here is the result:

You can download the code from http://mvvmjs.codeplex.com It’s in the folder misc\SampleTests

How all this works:

There are two main things:

  1. all test files ends with *tests.js. It’s configurable via jasmine.wwa.fileFilter. Example: jasmine.wwa.testFilesFilter = “*spec.js”;
  2.  unit testing project is configured to start test runner shell file: shell.html.

The shell loads and run ALL test files. A test file itself registers test suite and specs into the jasmine environment. Please note that shell runner follows M-V-VM style.

Future development

The shell runner will be updated quite much as MvvmJS is going to be extended with different aspects, e.g. binding extenions, controls, etc.

If you want to use it on your project, just copy the whole project and add your tests – the shell will discover your tests and you will be able to run them.

Happy testing on WWA!

About these ads

4 Comments »

  1. This looks really cool. Unfortunately, it seems it is not working with the current version of WinJS. I’ve included my error output below. Do you have any idea why these problems are occurring or how they might be fixed? Thanks.

    ‘WWAHost.exe’ (Script): Loaded ‘Script Code (MSAppHost/1.0)’.
    Exception is about to be caught by JavaScript library code at line 416, column 17 in ms-appx://microsoft.winjs.1.0/js/base.js
    0x800a139e – JavaScript runtime error: WinJS.Utilities.requireSupportedForProcessing: Value is not supported within a declarative processing context, if you want it to be supported mark it using WinJS.Utilities.markSupportedForProcessing. The value was: ‘function (source, sourceProperty, dest, destProperty) {
    var observable = source._getObservable && source._getObservable();
    var that = observable || source;
    var fn = source[sourceProperty];
    if(!fn) {
    return;
    }

    var handler = fn.bind(that);
    handler = safe ? safe(handler) : handler;

    if (dest.winControl) {
    dest = dest.winControl;
    }

    dest.addEventListener(destProperty, handler);
    }’

    Comment by Peter — January 29, 2013 @ 10:21 pm

    • Yeap, I’ll upgrade it and let you know today.

      Comment by Frantisek — January 30, 2013 @ 7:28 am

      • We review it and I’m going to refactor it quite massively. So I’d like to ask you for a passion, I’ll let you know when I’m done (max end of next week, but should be sooner)

        Comment by Frantisek — January 30, 2013 @ 9:31 am

    • Hi Peter, I finally upgraded the test runner to WinJS 1.0 version. You can download it from http://mvvmjs.codeplex.com from SampleTests project. Let me know if it works.

      Comment by Frantisek — March 1, 2013 @ 8:23 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: