Microsoft Articles

TDD / Unit Testing JavaScript in Visual Studio using Jasmine and Chutzpah

 

In this post we will see how to unit test JavaScript in Visual Studio using Jasmine and Chutzpah. We will use Visual Studio’s built-in Test Explorer window for displaying the results. Jasmine is a an open source unit testing / behavior-driven framework. It lets us write test cases. Chutzpah is an open source test runner. It runs jasmine tests, pulls in the results and displays them in Visual Studio’s Test Explorer window. It utilizes phantomjs framework, under the hood, for pulling in the results.

Let’s do some homework and create a bunch of JavaScript functions that we will unit test.

SimpleCalculator.js

var SimpleCalculator = function () {
    this.square = function (num1) {
        return num1 + num1; //logical error has been added here purposefully
    };

    this.sum = function (num1, num2) {
        return num1 * num2; //logical error has been added here purposefully
    };
};

 

Next, we will add some extensions / NuGet packages to perform unit testing. 

Adding Jasmine framework

1. Right click the project and click Manage NuGet Packages

2. Search for Jasmine and install it

image

3. Notice Jasime has added a bunch of files.

image

Installing Chutzpah extension

1. In Tools menu, click Extensions and Updates

2. Search for Chutzpah and install Chutzpah Test Adapter for the Test Explorer

image

3. After installing Chutzpah, restart Visual Studio

4. In Tests menu, click Windows > Test Explorer. This is where all tests will show up.

Create unit tests using Jasmine framework

1. Create another JavaScript file where we will organize tests for SimpleCalculator that we created previously. Let’s name the file SimpleCalculator_Tests.js. Here are the file contents:

//following two statements are adding references to Jasmine
//framework and our custom SimpleCalcualtor.js
/// <reference path="../jasmine/jasmine.js" />
/// <reference path="../SimpleCalcualtor.js" />
//describe lets us create a test suite / test class
describe("SimpleCalcualtor_Tests", function () {
    var sc = new SimpleCalculator();
//it function lets us create a test.This is for the sum function
    it("sum_test", function () {
        var num1 = 5;
        var num2 = 6;
        var expected = 11;
        var actual = sc.sum(num1, num2);
        expect(actual).toBe(expected);
    });
//this is for the square function
    it("square_test", function () {
        var num1 = 5;
        var expected = 25;
        var actual = sc.square(num1);
        expect(actual).toBe(expected);
    });
});

 

As soon as we write a test, it appears in the Test Explorer window. This is happening due to the Chutzpah plugin.

image

Running tests

In the Text Explorer, click Run All. Notice both tests have failed.

image

Go back to your original SimpleCalculator.js, fix the issues by changing operators used in sum and square functions and run the tests again. Notice, this time they have both passed.

image

Conclusion

Both Jasmine and Chutzpah are open source and make it very easy to unit test JavaScript code. It can be used with plain vanila JavaScript with libraries like JQuery and with frameworks like AngularJS etc.

No Comments

Web Application Compatibility Lab Kit

Internet Explorer has seen several iterations over the past 20 years. IE 11 is the last version of Internet Explorer. Microsoft Edge is the future of web browser which got introduced in Windows 10. Internet Explorer, in the past, was a Software-as-a-Product. Where as Microsoft Edge is a Software-as-a-service and will be continuously updated. IE 11 is recommended for legacy websites that relies on legacy binary extensions, such as ActiveX controls, and legacy CSS concepts, such as CSS expressions. Microsoft Edge is meant for the modern web HTML5/CSS3 etc. Ideally, all websites should be upgraded to the modern web standards but it’s a fact it’s a very resource intensive process. So, while we are upgrading older web sites to support the modern standards, we still have to satisfy customers by letting them use older web sites in a newer version of web browser. That’s where we have to rely on various compatibility options provided by the browser, such as Enterprise Mode, Enterprise Site Discovery etc.

Web Application Compatibility Lab Kit is a primer for the features and techniques used to provide web application compatibility  during migration. Download link is available on this webpage: https://technet.microsoft.com/en-us/browser/mt612809.aspx

No Comments

What are “Server Roles” / MinRole in SharePoint 2016?

Installation of older versions of SharePoint is agnostic of role. It’s the services that farm admins provision that determines the server role. E.g. on a search server, farm admins provision Search Service and the related services. Similarly different set of services are provisioned on an application server and front-end server.

 

SharePoint 2016 introduces a concept called MinRole, aka. Server Roles, which is designed to transform guidance to code. This concept helps in implementing best practices for optimizing performance for the role type. MinRole/Server Role can be configured both by using powershell (PSConfig) and the gui (PSConfigUI)

clip_image003

1 Comment

What are “Server Roles” / MinRole in SharePoint 2016

Installation of older versions of SharePoint is agnostic of role. It’s the services that farm admins provision that determines the server role. E.g. on a search server, farm admins provision Search Service and the related services. Similarly different set of services are provisioned on an application server and front-end server.

 

SharePoint 2016 introduces a concept called MinRole, aka. Server Roles, which is designed to transform guidance to code. This concept helps in implementing best practices for optimizing performance for the role type. MinRole/Server Role can be configured both by using powershell (PSConfig) and the gui (PSConfigUI)

clip_image003

No Comments

Windows 10 SDK update

Next year should be a great year for .NET. One of the prominent features is iOS to Windows bridge that will most likely get released by next year. WinObjc lets you write Windows applications using Objective C and it also allows migration of existing iOS applications over to Windows. Apple has also made Swift open source so you never know it might also end up becoming available on Windows.

https://blogs.windows.com/buildingapps/2015/12/03/whats-new-for-developers-in-the-windows-10-sdk-build-10586-update/?wt.mc_id=DX_47478&MC=IoT&MC=DevOps&MC=MSAzure&MC=IE&MC=JavaScript

Evaluation developer virtual machines can be downloaded here: evaluation developer virtual machines (VM) on Windows Dev Center

No Comments

Integrating Visual Studio with JIRA Software

JIRA Software is a web-based agile project management solution. It offers great support for integrating with various development tools. If you don’t want to use the web interface for managing issues then you can use the plugins and connect to JIRA via development tool. In this post we will see how to integrate Visual Studio with JIRA Software.

1. In Visual Studio access extensions and updates

image

Search for VSJira and install VSJira

Screenshot_2015-12-21__3_37_PM

 

Configure VSJira

Screenshot_2015-12-21__3_40_PM

image

Enter server url, username and password.

image

 

Create a new issue

image

Screenshot_2015-12-21__3_58_PM

…and you are good to go!

, ,

No Comments

JSON support in SQL Server 2016

SQL Server 2016 is around the corner. XML has been supported since SQL Server 2008. In SQL Server 2016 although JSON is not natively available as a data type, but, there are boat-load of functions and keywords that lets you utilize JSON data.

 

Data Type

Data type wise, JSON is represented by NVARCHAR data type. Main reason is people have been storing JSON data for quite some time now and migration will be a lot easier if data types aren’t required to be changed.

 

Querying a relational table and returning result in JSON format

If you want to query some existing table and return result in JSON format then you can use similar syntax that’s used for returning data in XML format.

SELECT col1, col2 FROM table FOR JSON [AUTO | PATH];

If you use AUTO switch, it will use default behavior for returning column names and arrays.

For having more control over names and arrays you can use PATH switch

So, essentially you are converting data to JSON format at database layer rather than at service layer.

 

Querying JSON data and returning result in a relational table format

If you already have JSON string and you want to convert it to a relational table then OPENJSON can be used, just like OPENROWSET can be used for querying a CSV / Excel file.

Let’s assume we have employees variable that contains collection of employees.

Let’s say we have employees data stored in a variable (or some table’s column) like this:

DECLARE @employees nvarchar(100)

SET @employees ={ ‘employees’:[

 {‘EmployeeId’:1,EmployeeName’:’Bob’},

 {‘EmployeeId’:2,EmployeeName’:’Alice’}

]}

 

The following query will let us query JSON data as a relational table:

 

SELECT EmployeeId, EmployeeName

FROM OPENJSON (@employees, ‘$.employees’)

WITH

(

  EmployeeId int,

  EmployeeName varchar(20)

) AS EmployeesArray

 

Other JSON related built-in functions

ISJSON(json_data)

It’s a useful function for validating JSON.

JSON_VALUE(json_data, path)

This function returns a scalar value (some specific column/property’s value) from JSON data.

e.g.

JSON_VALUE(@employees, ‘$[0].EmployeeId’) returns employee id of first record.

JSON_VALUE(@employee, ‘$.employeeId’) returns employee id of the record ASSUMING that @employee is a single record / object and not an array.

Indexing on JSON data

Since there’s no new data type for JSON data, so you can use regular indexes for your JSON based columns.

,

2 Comments

Changes in SharePoint 2016 installation and deployment

SharePoint 2016 is around the corner. There are some installation and deployment changes. You can read up on the details here: http://blogs.technet.com/b/wbaer/archive/2015/05/12/what-s-new-in-sharepoint-server-2016-installation-and-deployment.aspx

, , ,

No Comments

First major Windows 10 Update is now available

First major Windows 10 update is now available. It makes Windows 10 faster and more secure than ever. You can read details here: http://blogs.windows.com/windowsexperience/2015/11/12/first-major-update-for-windows-10-available-today/?OCID=WIP_r_Body_Blog_LatestPC

,

No Comments

How do I reduce CSS/HTML/JavaScript footprint in Visual Studio?

Creating SPAs, AJAX enabled apps definitely speeds up web applications. If you want to take performance to next level then you can reduce the CSS/HTML/JavaScript footprint. The smaller files web browser has to download the faster performance will be. If you are using Visual Studio then you can download VS Web Essentials for free. After installation just go to any web project where you have CSS/HTML/JavaScript files. Here’s a sample project:

image

Right click the files and use Minify File. It will generate minified version of these files, provided there’s no syntax error.

image

Just make sure you reference .min version of js and css files.

Magnification happens on the fly. On day to day basis you will still modify regular .css/.html/.js but you will reference and deploy minified version of these files.

In case if you want to stop minifying a file just right click it and select Remove from bundling… option.

When it comes to web site performance size does matter. Good things come in small packages.

No Comments