PhantomJS not exiting for VSTS hosted builds

I wanted to use Karma to run some legacy AngularJS Jasmine 1.3 unit tests on a VSTS hosted build. There were two problems. First, the latest karma-jasmine wouldn’t work, because Jasmine 2.x no longer supports “runs” and “waitsFor”. The solution is to use karma-jasmine@0.1.0, which comes bundled with Jasmine 1.3. So that solved the problem of the Karma adapter for Jasmine. But which version of Karma corresponds to karma-jasmine@0.1.0? After some digging, we found that karma@0.12.37 works fine.

The second problem, which was far thornier, was that PhantomJS worked fine locally, but not on the VSTS hosted build. PhantomJS simply wouldn’t exit, which caused the build to time out. The solution is to check in phantomjs.exe into source control, and to create and check in a batch script. The batch script does two things: set the PHANTOMJS_BIN environment variable to point to that phantomjs.exe, and run Karma. The detailed steps are as follows:

  1. In a temp folder, run npm install phantomjs-prebuilt.
  2. Copy phantomjs\node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs.exe from the temp folder to a folder under source control, and check it in.
  3. Create and check in a file named karma-runner.cmd that has the following contents:
@echo off
REM To run this in your local machine, set BUILD_SOURCESDIRECTORY=C:\path\to\git\repo\folder
%BUILD_SOURCESDIRECTORY%\path\to\.bin\karma start %BUILD_SOURCESDIRECTORY%\path\to\karma.conf.js
  1. In the VSTS build definition, add a step of type Batch Script that points to karma-runner.cmd.

10 October 2017 | Node.js | Comments