How to run the JavaScript unit tests from Visual Studio.

In my previous post How to set up JavaScript testing in Visual Studio 2017 with Jasmine and Karma I created a Visual Studio 2017 suite for running JavaScript unit tests.

In that post I started the process of running the tests on the command prompt. There is also a way to start them from Visual Studio itself.
You can do that with the help of a taskrunner named Gulp.
Gulp is a tool you have to install; I’ll do that with the help of NPM.

Run this command in the directory your karma.conf.js file is located:


Npm install -g gulp --save

 

Now add to your project, at the same level as the karma.conf.js file, a new JavaScript file named gulpfile.js.

(I’m not sure why it must be called gulpfile.js or that it is completely mandatory to do so, but when I renamed the file it didn’t work anymore).

Then add the following content to this file:


var gulp = require('gulp');

var Server = require('karma').Server;

gulp.task('start-javascript-unit-testing', function (done) {
 new Server({
 configFile: __dirname + '/karma.conf.js',
 singleRun: false
 }, done).start();
});

This will create a gulp-task with the name  start-javascript-unit-testing.

This task will start a Karma server and then wait for changes in your (test-)code (like specified in the karma.conf.js file.

You will then also see this task in your task runner:

task-runner-explorer

If you don’t have the task runner visible, use [view] [other windows] [task runner explorer] to show it.

If you double-click the task start-javascript-unit-testing a small window will be opened and the first run of tests will start.

task-runner-explorer-start-1

Then, when you change the (test-)code the tests get re-executed for direct feedback.

task-runner-explorer-start-2

Happy unit testing!

In the install of gulp, I used the -g switch which installs the package globally.

The NPM docs say “If you want to use a package as a command line tool, then install it globally. This way, it works no matter which directory is current. This is the choice you would use if you were installing grunt, for example.”; and grunt is sort of gulp.

But when I wanted to use the (globally installed) gulp in another project, I did have to install it local to that project too:


Npm install gulp --save

Advertisements

2 thoughts on “How to run the JavaScript unit tests from Visual Studio.

  1. Pingback: How to set up JavaScript testing in Visual Studio 2017 with Jasmine and Karma | Affinno's blog

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