Geeks With Blogs
Carl Hembrough
| Home |
My goal here is to run Javascript unit tests as part of a Travis CI build process. The project is Java based, so the Travis environment is configured for Java. I need to find a clean way of running my Jasmine tests as part of the build using npm. 

Up and running with Jasmine

Our Javascript build is purposely restricted to NPM only. To get up and running with Jasmine I used the following steps from the Jasmine NPM package page.

Install the Jasmine NPM package:
$ npm install -g jasmine

Create the folder structure for jasmine and add example tests:
$ jasmine init
$ jasmine examples

Run Jasmine
$ jasmine

Your console should show the test results as follows: 
5 specs, 0 failures
Finished in 0.008 seconds

The tests are now running so we are ready to integrate with the build process.

Build process integration

I added Jasmine as a dev dependency in my package.json: 

$ npm install jasmine --save-dev

This ensures other developers on the project do not need to install jasmine manually. Our current build process runs npm install so the installation will be taken care of automatically.

I now need to add a command into the scripts section of package.json to run the tests. The npm test command is a widely used convention for running tests, so I will use this.

Our package.json now looks like this (with other unrelated things taken out):

"devDependencies": {
"jasmine": "^2.2.1"
"scripts": {
"test": "jasmine"

You can see jasmine is listed as a dev dependency, and we have a test command under the scripts section configured to run the jasmine command.

Now when I run npm test I am given the test same test results as before when I ran jasmine on its own.

Travis integration

As mentioned before the project is Java based and our .travis.yml file defines a Java environment. Thankfully Travis runs node.js on all environments, so it is already available to use in the environment.

Looking at the travis build configuration docs, I want to run npm install under the before_script section, and to execute the tests under the script section.

Typically with node projects there is a package.json in the root of the project. Our package.json file is located under src/main/web, which is a convention of the Java build tool Maven. To run npm under a different directory the --prefix option is used. 

Our .travis.yml now looks like this:

language: java
jdk: - oraclejdk7 before_script: - npm --prefix src/main/web/ install script: - npm --prefix src/main/web/ test
When I checked this update into GitHub, the travis build started and failed. Looking at the logs the npm install command worked fine, but the npm test command failed: 

$ npm --prefix src/main/web/ run test

npm ERR! Error: ENOENT, open '/home/travis/build/repo/project/package.json'

After applying the fix, the .travis.yml file now looks like this:

language: java
- oraclejdk7
- '[ "${TRAVIS_NODE_VERSION}" != "0.8" ] || npm install -g npm@1.4.28'
- npm install -g npm@latest
- npm --prefix src/main/web/ install
- npm --prefix src/main/web/ test

The tests pass and the build is green again!

> jasmine



5 specs, 0 failures

Finished in 0.006 seconds

The command "npm --prefix src/main/web/ test" exited with 0.

Done. Your build exited with 0.


Posted on Wednesday, April 22, 2015 9:34 PM NPM , Git , travis , javascript , unit testing , java , jasmine | Back to top

Comments on this post: Integrated Javascript testing with Jasmine, Travis, and NPM

# re: Integrated Javascript testing with Jasmine, Travis, and NPM
Requesting Gravatar...
Your website provides us relevant informations about different programming styles, such as C, Java, MySQL and others. This variety supplies all the needs that we may take into consideration on working in this particular area. Programming is not a simple activity and for making a living from this aspect, I have to be a real good professionist.
Left by Julia on Apr 28, 2015 10:04 AM

Your comment:
 (will show your gravatar)

Copyright © Carl Hembrough | Powered by: