Wednesday, December 27, 2017

How to run JavaScript unit tests interactively from any text editor

Overview 


This blog describes the procedure to install gulp-jasmine-livereload-task, to be able to execute on the fly JavaScript Jasmine unit tests from any text editor.

With this gulp task, you can update a Jasmine unit test file and once saved on the disk, it is executed in a browser, giving the result.

Steps


Installation 

C:\>md test
C:\>cd test
C:\test>md src
C:\test>npm install --save-dev jasmine-core gulp-jasmine-livereload-task
C:\test>npm install --save-dev gulp-debounced-watch
C:\test>npm install gulp

Gulpscript

In the test folder create a a file name gulpfile.js. Copy paste the following
var gulp    = require('gulp'),
    jasmine = require('gulp-jasmine-livereload-task');
 
gulp.task('default', jasmine({
    files: ['./src/**/*.js', './spec/**/*.js'],
    watch: {
        options: {
            debounceTimeout: 1000, //The number of milliseconds to debounce. 
            debounceImmediate: true //This option when set will issue a callback on the first event. 
        }
    }
}));


JavaScript unit test files

Create the file test.js in src folder and copy this content
describe("Suite", function() {
  
    it("Test", function() {

      expect(true).toBe(true);
    });
});


Execution

How to run the scripts

From the command line:
C:\test>gulp
This will load the default browser with the output of the unit test(s) execution

No comments:

Post a Comment