Jasmine JS – Introduction

I can add Jasmine JS to a VS solution from NuGet.

Once downloaded Jasmine I can write a simple set of tests (in a dedicated JS file) in the following way:

describe("MyTests", function () {
    it("This is the test 1", function () {
        expect("this is a test string").toContain("test");
    it("This is a test with positive result", function () {
        expect(2 * 4).toEqual(8);
    it("This is a test with negative result", function () {
        expect(2 * 4).toEqual(9);

I can run JS tests directly from VS with Resharper (otherwise other extensions are available, such as Chutzpah). With Resharper I can run a single test or the entire test set by right-clicking on the text editor. The results will be displayed in both the text editor and the Resharper Unit Test window.

Executing code before each test

We can use the beforeEach(function() { … }) method to define the code that should be executed before each test.


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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s