How to test your Node.js app against multiple versions of Node

- By Abhijit Kini


Software applications are regularly updated primarily to add new features, resolve bugs in the code and for security enhancements. A development language is no exception and each major update is called as a version. While it is a great advantage to have newer features, lesser issues and stronger security, the challenge arises when complexity creeps in. With multiple versions of a language, applications that depend on them have to be compatible with many versions. Moreover, when the applications themselves have to be updated, the combination of different versions of the language and the application makes it a challenge to write and test code.

Testing code against multiple versions of a language early in the application delivery life cycle helps in fixing errors quickly and makes testing more efficient in the downstream of the life cycle. In this tutorial, I'll go through testing a Node.js application against multiple versions of Node.js for every code commit during the continuous integration phase of the software development life cycle.

Before you begin

Ensure you have:

  • Signed into Shippable using your GitHub or Bitbucket account.
  • An enabled project written in Node.js hosted onGitHub orBitbucket.

Configuring the shippable.ymlfile

The scenario that I would like to go over is to test the Node.js application against three Node.js versions - 0.10, 0.12 and 4.2.3. My shippable.yml file is shown below:

language: node_js

  - 0.12
  - 4.2.3
  - "0.10"

  - XUNIT_FILE=shippable/testresults/result.xml

    - npm install
    - npm test

    - ./node_modules/.bin/istanbul cover grunt -- -u tdd
    - ./node_modules/.bin/istanbul report cobertura --dir  shippable/codecoverage/

  • language: specifies the language I use. Node.js in this instance. The syntax is node_js.
  • node_js: here's where I specify the versions. I have specified the 0.12, 4.2.3 & 0.10 in a particular order. The builds will be tested in the same order.
  • env: This is an optional setting for this scenario. I have specified an environment variable to pipe out the test results.
  • build: ci: This is the section that executes continuous integration tasks. Here I've included npm install to ensure dependencies are installed. npm test kicks of the unit test.
  • post_ci: This section executes tasks after successful completion of continuous integration tasks. The ./node_modules/.bin/istanbul..... settings are optional and are there to show case code coverage reports.

With these changes in the shippable.yml file, I'll push this code to my GitHub repository by running the commands git add .; git commit -m "added 3 node versions" ; git push origin master from my local directory using a Terminal. This single code commit triggers a matrix build on Shippable where the entire build process, including tests, is run for all 3 versions of Node.js. The output, with a tab for each version (49.1, 49.2, 49.3), is shown below:

Testing multiple versions of node.js in a single Continuous Integration build


While specifying the language versions in the shippable.yml file, you can use a number format (0.12) or a string format ("0.10"). In scenarios where the version ends with a 0, such as 0.10, it is safer to use the string format to avoid the yml parser from translating the version to 0.1.

The above scenario of running tests on multiple versions of a language, with a single code commit (or a pull request) is just the beginning. You can include multiple environment variables in the shippable.yml file, and configure tests using a combination of language versions to environment variables. I'll cover this in a later tutorial. In the mean time, you can refer to our documentation on this topic.

I've now successfully tested and run Continuous Integration on my Node.js application against three versions of Node.js. Try it yourself and let me know in the comments below, what you think of this capability.

Try Shippable

Topics: continuous integration (CI), how-to, nodejs, tutorial, unit test