RSS

Category Archives: NodeJs

Configuring Bamboo to Show Jasmine Test Results

If you already got unit test setup that has been configured to run with karma in local environment it might be a good idea to integrate unit test suite with your continues integration(CI) server.
You can follow these steps to configure popular Bamboo build server to integrate unit test suite written with Jasmine.
1. First step on integrating with Bamboo is to generate test results file that can be understood by Bamboo server. To do that first need to go to configure build plan page in Bamboo server as shown below.

bamboo-configure-plan2. In the configuration page Locate the build job and go to job tasks configuration by clicking on the job.

job-configuration-in-bamboo

task-configuration3. Click on Add task to add a task.

4. In the add tasks configuration dialog for the selected job, we have few options to parse jasmine test results. We can add JUnit test parser, Mocha test parser etc. in this case I will show how to use Junit test parser.

task-types

5. After you add the Junit Test parser your task configuration might look similar as shown below It is super important to make sure that you add Junit test parser as a “Final Task in Bamboo”. You should ideally add them after “Final Tasks” gray bar as shown below

junit-parser-task

In JUnit parser task configuration, you should specify test reports XML path which I will describe later.

What are Final Tasks in Bamboo?

Test result parser tasks like these should always run regardless of when previous build tasks successfully ran or not. The reason is when one of unit test get failed previous build tasks might get failed but still you need to run test parser task since you need to see the failed tests too.

So in above steps we have mostly completed Bamboo configuration steps. Next we need to do is generate JUnit test result xml from jasmine tests through npm command line. This post assumes you already have Jasmine unit tests configured to run with Karma in your local environment. Covering that is beyond the scope of this post and you can find more information on that in here if you need to. If you have Karma test runner already setup you can add Karma JUnit reporter as shown below.

6. First install “karma-junit-reporter” to your unit test project with below command.

[source-code language=”java”]npm install karma-junit-reporter –save-dev[/source-code]

7. Add following to the Karma configuration. Make sure you define options for JUnit reporter as expected.

junitReporter: {
    outputDir: 'test-reports', // results will be saved as $outputDir/$browserName.xml
    outputFile: 'junit-report.xml', // if included, results will be saved as $outputDir/$browserName/$outputFile
    suite: '', // suite will become the package name attribute in xml testsuite element
    useBrowserName: true, // add browser name to report and classes names
    nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
    classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
    properties: {} // key value pair of properties to add to the section of the report
}

Important thing to note in this test reporter configuration is that make sure the output path you give in this step is correct as expected in above “JUnit Parser” custom result directories path as shown below.

tasks.png

8. Sample karma.conf can be as below. You might need to make sure “singlerun” to true so that test run will not hang up the build.

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        files: [
            'your files here...'
        ],
        preprocessors: {},
        reporters: ['progress', 'spec', 'kjhtml', 'junit'],
        // web server port
        port: 9876,
        browsers: ['Chrome'],
        singleRun: true,
        concurrency: Infinity,
        // the default configuration
        junitReporter: {
            outputDir: 'test-reports', // results will be saved as $outputDir/$browserName.xml
            outputFile: 'junit-report.xml', // if included, results will be saved as $outputDir/$browserName/$outputFile
            suite: '', // suite will become the package name attribute in xml testsuite element
            useBrowserName: true, // add browser name to report and classes names
            nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
            classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
            properties: {} // key value pair of properties to add to the section of the report
        }
    })
}

8. In your build step you can run following command to run unit tests. This will generate test report
[source-code language=”java”] karma start[/source-code]
As you would understand now after the build task completes in Bamboo job, we have got the JUnit test results xml already saved to the disk. In the JUnit test parser task which runs after build task, these test results will be parsed and shown in the bamboo build results page.
In the build dashboard you will see test summary in in “Tests” tab as shown below…

build-results-1

If you need to see the test results you can click on “Job link” and see more test run details.

build results 2.png

build-results-3

If you have failing test you will see that build get failed and failed test results will be shown in build results page.

failed-build-results

If you do not see these test results when unit tests get failed, make sure you have added “JUnit Parser” as a Final task as described above in Bamboo configuration. If you have Mocha as test runner instead of Karma you do not need a converter task. Instead you can directly use Mocha test parser in Bamboo and the result of the results are much similar.

Advertisements
 
3 Comments

Posted by on February 23, 2017 in ASP.NET, Bamboo, Jasmine, NodeJs, Unit Testing

 

Tags: , , , , ,

Connecting to SQL Server from NodeJs

With NodeJs it is more common to use MySQL, MongoDB, PostgreSQL than Microsoft SQL server as the database server, at least what I have encountered. However if we happens to prefer using Microsoft SQL with NodeJs there are few of options we could use.  Advantage of using NodeJs server for SQL server communication is it can be hosted in any platform with minimal issues.

Using an ORM ~ Using Sequelize?

Sequelize is a great ORM utility to use with NodeJs which also has the advantage of having abstraction layer for separate database drivers to interact with Microsoft SQL server, PostgreSQL, MySQL, MariaDB, SQLite and MSSQL. This is by far the best option we had so far. Other major benefit is the friendly API that it supports with promise based API which many NodeJs, AngularJS developers are following these days. Sequelize uses appropriate NodeJs package internally for each database server.

For Microsoft SQL server tedious is the node package to be used with Sequelize. Following two steps should be followed to getting started with connecting SQL server from NodeJs.

npm install --save sequelize //sequelize package

npm install --save tedious // SQL server TDS driver

Connecting SQL Server using Tedious without ORM?

Tedious is the npm package that is being internally used by Sequelize to connect to SQL server. This actually uses Tabular data stream protocol(TDS) for connecting to SQL server natively. Another compelling reason to use Tedios is that it is being actively contributed by Microsoft from recent past.

If you do not prefer using ORM for connecting to SQL server from NodeJs, you can also use Tedious without using Sequelize by using following steps. Tedious also supports SQL Azure by supporting encryption.

var Connection = require('tedious').Connection;

  var config = {
    userName: 'test',
    password: 'test',
    server: '192.168.1.210',

    // If you're on Windows Azure, you will need this:
    options: {encrypt: true}
  };

  var connection = new Connection(config);

  connection.on('connect', function(err) {
    // If no error, then good to go...
      executeStatement();
    }
  );

Connecting to SQL server from Sequelize

Connecting to SQL server from node js is simple you can abstract this in your factory class in a way that consumer code of backed database will be unaware about the database server completely.

var sequelize = new Sequelize('database name', 'username', 'password',
	{ host: 'localhost', dialect: 'mssql',
	  port: 1433,
	  pool: { max: 5, min: 0, idle: 10000 },
	  dialectOptions: { instanceName: 'instancename'}
	});

Any database driver specific code as above can be passed to dialect options.

 
Leave a comment

Posted by on December 14, 2016 in ASP.NET, MS SQL Server, NodeJs

 

Tags: , , , ,

Automatically Restart Node For Application Changes With Grunt

When you are building applications with node at start it might be very annoying to restart node js server when you do a change to a js file. Each time you do a server side non public Js file change you need to restart server to reflect those changes to be seen in client side(browser). This is not necessary to static resources.

One quick way to avoid this workflow is to use grunt to restart your server when there is a file change in your js files. If you are new to grunt, Grunt is node package more of like a build tool that can be used to run various build time tasks in your applications.

First step is installing latest version of grunt in your project. Here make sure you save this as a dev dependency but not as an actual dependency.

npm install grunt  --save-dev

Secondly you have to make sure grunt command line client is installed.

npm install grunt-cli –g

To monitor server side js changes you need to install grunt node monitoring package.

npm install grunt-nodemon  - -save-dev

In your NodeJs project root you need to create gruntfile.js which is basically acts as configuration file for grunt.

node project

You can enter grunt file content as following.

module.exports = function(grunt) {

  grunt.initConfig({
    nodemon: {
      all: {
        script: 'app.js',
        options: {
          watchedExtensions: ['js']
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-nodemon');
  grunt.registerTask('default', 'nodemon');
};

Your application entry point Js file should be included for node mon’s “script” config property value in my case which was “app.js”. Once you have done this all you have to do is just type “grunt” in you terminal.

node autorestart

You can enter “rs” to restart node at any time if you change a server side non public js file you would see nodemon is restarting the node server avoiding you to do it manually and avoiding the annoyance as you would see in terminal window.
node autorestart 2

 
Leave a comment

Posted by on December 25, 2015 in ASP.NET, NodeJs

 

Tags: ,