Monthly Archives: March 2011

ReSharper 6 Introduces Support for JavaScript Unit Testing with QUnit

In ReShaper 6 we have taken the first step to make it easier for developers to work with JavaScript. By providing support for analysis, refactoring and code-navigation, we try and remove some of the difficulties developers might encounter when working with this language. Now we’re taking the next step!

Running QUnit from ReSharper

When it comes to Unit Testing, even some of the more experienced JavaScript developers tend to not pay too much attention to testing their code. Reasons for this include too much friction, lack of understanding on how to test, what framework to use, etc. Although we can’t solve all of the problems, we are going to try and remove a few of them by providing support for running JavaScript Unit Tests in an easier way with ReSharper. And we are starting this by providing support for QUnit, the unit testing framework used by jQuery. You will be able to run QUnit JavaScript tests using the same interfaces you know and love in ReSharper. And the best part of it all, is that it’s really easy! Let’s see how.

1. Define the code under test

First step is to have some code that we want to test. This code can be located in the same file where are tests are (not recommended) or in different files. In our case, we want to test some simple calculator operations:

image

We have placed this code in a file called CodeUnderTest.js but it can be named absolutely anything.

2. Write some tests

Writing Unit Tests in QUnit is pretty straightforward. Whereas with most test frameworks, you’d normally write a class that contains test methods, with QUnit, you call a function test which takes two parameters, a description of the test and the actual code to test, which is passed in as a function:

image

We place this code in a file called CalculatorTests.js (again, can be named anything). Our project structure now looks like this:

image

Since our tests are located in a file different to that of our tests, we do need to tell ReSharper which code we are referring to by using a specific reference notation at the top of the test file:

image

As soon as we add the tests, ReSharper will detect that these are QUnit tests and provide us with the ability to run them by placing the Test Runner menu in the left gutter:

image

providing us with the ability to run the tests:

image

We can now run individual tests and get the output displayed in the ReSharper test runner:

image

Notice how the tests are grouped under “Calculator tests”. This is done using the module function which allows grouping of tests:

image

When the tests are run, ReSharper launches the default system browser which runs the tests and reports the information back to ReSharper. Normally, to run QUnit tests, you would need to create an HTML file that runs the them. This is handled internally by ReSharper, so it’s one less step to perform.

image

If you prefer to use a specific browser to run the tests, you can do so by changing the browser under the ReSharper Options | Tools | Qunit

image

There is still more to come…

Currently, ReSharper is bundling the latest available version of QUnit (at the time of building). We will be adding support in upcoming releases which will allow you to use your own version of QUnit. Also, we will be looking at providing access to the HTML file that ReSharper generates, allowing better support for Continuous Integration environments, as well as the possibility of running the tests from the browser directly.

When can you play with it?

Right now! The public EAP build includes this functionality. Download it from here the ReSharper 6 Early Access page.

And as always, feedback welcome. Feel free to provide comments here or using our issue tracking system where you’ll not only be able to monitor the status of a feature but also get people to vote for it.

ReSharper enhances suggestions with explanations

 

One of the new features we have added to ReSharper 6 is the ability to find out why ReSharper makes certain suggestions. Often, ReSharper can provide a suggestion such as the one shown below:

image

For some people this might be clear, whether or not they would agree with the suggestion. For others it might make little sense, specially developers that are often new to the language or framework. In ReSharper 6, we have added a feature* which provides more information as to why these suggestions, hints or warnings are made:

image

When invoking the QuickFix, by pressing Alt+Enter, ReSharper now provides a new entry which is “Why is ReSharper suggesting this?”. By clicking on this entry, you will be redirected to a Wiki on the JetBrains site where you will be provided with more detailed information in regard to the suggestion. For instance, when being displayed with the following:

image

by selecting the “Why is ReSharper suggesting this?” action, we will be displayed with the following information:

image

Over 40 Entries done, more to come

Currently we have over 40 entries in the Wiki and are continuing to add more entries. We will also be updating and reviewing them with more examples were required. As always, please let us know if you have any feedback.

 

* There is a plug-in which is available for previous editions of ReSharper called WhySharper, which this idea was partially based on. We realized however, that to make sure the information is kept up to date, it would be better to have this functionality built into the product.

JetBrains night at Bristol .NET User Group

 

On the 14th of March I’ll be doing a few sessions at the Bristol .NET Usergroup. It will be a talk focused on ReSharper and someimage of our other tools. For ReSharper we’ll see how to use it effectively for TDD, BDD as well as following standards across teams and detecting code smells using things such as Structural Search and Replace, advanced refactoring and code navigation. The talk is going to be exclusively inside Visual Studio. I’ll also cover some aspects of Code Coverage and showing alternative solutions to tools like TFS by combining the power and simplicity of TeamCity and YouTrack and Open Source software.

This is a completely free event, doors open to all and there is no registration required. The event starts at around 18:30 on 14th March. Meetings are held at:

University of the West of England (UWE)
Frenchay Campus, Coldharbour Lane,
Bristol, BS16 1QY (0117 965 6261).

We will be in room 2Q49 (in Q block). Remember, it’s the 14th of March, 18:00 – 18:30

You can find directions to the university at http://www.uwe.ac.uk/aboutUWE/campus/frenchay-directions.shtml. When you get to the university the guard can give you a map of the university to find Q block or you can find one at http://www.uwe.ac.uk/aboutUWE/campus/frenchay-map.shtml.

Park in Car Park 1 (and remember to bring a map to get to the room).