Friday 25 September 2015

Running Jasmine JS Tests on Jenkins with PhantomJS

Welcome to the latest JenkinsHeaven post!
I'm extremely pleased to be able to write this post for you. Running Javascript tests as part of your Jenkins build with Jasmine 2.2.0 is actually, and refreshingly, pretty easy.
  1. Download and install PhantomJS (currently v2.0) from here.
  2. Copy the contents of the zip file to C:\Program Files (x86)
  3. Create an environment variable called PHANTOMJS_HOME with value C:\Program Files (x86)\phantomjs-2.0.0-windows.
  4. Add %PHANTOMJS_HOME%\bin; to the beginning of the Path environment variable.
  5. At this point we want to test that PhantomJS is installed properly. Open a Command Prompt and execute phantomjs -h. You should see the PhantomJS help output. If you saw that, we're good to continue.
  6. Create a new Freestyle job in Jenkins, setting the Git Repository URL to https://github.com/detro/phantomjs-jasminexml-example
  7. Add a Windows batch command build step with the following two commands:

  8. del jasmineTestResults /s /q
    phantomjs test/phantomjs_jasminexml_runner.js test/test_runner.html jasmineTestResults/

    No need to include exit %%ERRORLEVEL%% as phantomjs_jasminexml_runner.js takes care of this for you internally.

  9. Add a Publish xUnit test result report post-build action
  10. In this post-build action click the Add button and select JUnit
  11. Set the JUnit Pattern to jasmineTestResults/*.xml
  12. In the Failed Tests section set all 4 Build Status Thresholds to 1
  13. Save the build configuration
  14. Run the build
  15. Pat yourself on the back, because 7 tests ran!
Hopefully you'll see something like this when you drill into your job. :)



I'll leave it as an exercise for you to take a look at the example code to see how it is structured. Should be easy for you to transpose to your own project.

The key files to look at when considering how to transpose to your own project:

  • src/tv.js
  • test/test_runner.html
  • test/test_spec.js

test/phantomjs_jasminexml_runner.js does not have to be altered at all.

Till next time...

No comments: