Wednesday, May 11, 2016

Test-Driven Development with N4JS

The N4JS IDE validates and checks code as you type, making it a powerful tool for writing type safe applications that that are predictable, reliable and easily maintainable over time. This means that problem-solving takes place before code is deployed and your applications are safely exported.

As test-driven development is a key technique for the development of large-scale projects, the N4JS IDE is bundled with a robust test framework called Mangelhaft. Mangelhaft is an xUnit-like framework with an engine for executing tests and will report results to the IDE while running test suites.

Testing with Mangelhaft in the N4JS IDE 

While Node.js projects written with N4JS can easily be checked and tested with Mangelhaft within the IDE, the release of Mangelhaft on npm means that your Node.js projects will stay stable when deployed on a Continuous Integration server.

Using Mangelhaft with Travis CI

Using the example project bundled with the N4JS IDE, we can demonstrate how to use Mangelhaft with a Travis build. In the Travis .yml file, we define the language of the build as node_js. Currently, Java8 must also be installed as a requirement for the build:

    sudo: required
    language: node_js
    before_install:
      - sudo apt-get -qq update
      - sudo apt-get install -y oracle-java8-set-default
    node_js:
      - "5"
    services:
      - mongodb
  

Mangelhaft can either be installed globally ( npm install -g n4js-node-mangelhaft ) or integrated with a build by including Mangelhaft in the devDependencies of a project and exporting as an npm package. Mangelhaft will be downloaded and installed automatically when the Travis build begins:

Installing Mangelhaft on Travis CI Server

Default test scripts described in the package.json can then be run with the npm test command which calls n4js-mangelhaft.

Test results in Travis CI

A full list of available options can be listed with the command n4js-mangelhaft -h

Using Mangelhaft with Jenkins

Mangelhaft can also be integrated with a Jenkins job and fully supports detailed xUnit test results. View the status of your tests over time with the Jenkins UI and benefit from individual test reports:

n4js-mangelhaft \
      …
      --xunitReportFile ./build/tasks-test-report.xml \
      --xunitReportName tasks-test-report \
      --xunitReportPackage n4js.example.tasks.tests \
      …
  
Mangelhaft Test Results in Jenkins UI 
Mangelhaft Test Results in Jenkins UI

Generate comprehensive xUnit test data as frequently as required and drill down to the root of any issues that arise:

Individual Test Results in a Jenkins Build

Type Safe Workflow with Mangelhaft

Running npm tests with Mangelhaft ensures Node.js projects created with N4JS stay stable with errors detected and located quickly and easily. Download the N4JS IDE and begin creating type safe Node.js projects with the peace of mind of CI tests made possible with Mangelhaft.

Resources referred to in this post