Engine of a Mini Seven racing Mini
Photo: Copyright © 2009 Eelke Blok

Running Drupal Javascript tests on Vagrant

The last three days, I was at the code sprint for the Drupal Camp Munich 2016, hosted at the Hubert Burda Media offices. It was a great experience. It was my first community code sprint, I've met some great people and I've learned a ton in three days. I have to thank the organizers of the sprint as well as the camp organizers, and of course my employer One Shoe to give me this opportunity. We have been working on the Media Initiative, to get a solid Media solution into Drupal core in an upcoming minor release (e.g. 8.3 or 8.4). Christian Fritsch, one of the main developers for the Media Initiative, asked me to have a look at one particular issue that is quite important to get sorted out, because it aims to solve a weird usability problem that certainly wouldn't fly for a core component. I've been able to push it quite a bit forward in those three days. Until I got to getting Javascript tests to run on my setup, that is.

There are some solid tutorials available to get you up and running with Javascript testing for Drupal. The one I was pointed to is this one from Alex Pott. Everything seemed to go OK, until I actually went to run the tests. The test would consistently get a 403 when trying to go to the login page of the test environment. When running the test from my Mac directly, the Simpletest install didn't seem to spin up correctly. In the end, it seems my setup with Vagrant is at the heart of the problem.

Giving this some thought*, I realised that the test environment having to run on the Vagrant box, while the test itself was running on the Mac was the likely culprit. The tutorial stresses that it is important that the tests are ran with a user with the proper permissions. Presumably, because it will need to initialize the environment which will then need to be accessed by the webserver to run the actual test. So, what do you do? Install phantomJS on Vagrant? Sure, that would have worked, most likely. But what worked too, was just setup a little port forward from port 8510 on the Vagrant box to the same port on the host:

ssh -L 8510: localhost

This is issued on the vagrant box. You may need to change the IP address of your host, if it is different, and you will need to provide the password for the vagrant account on the vagrant box (which usually is just "vagrant").

I both love it and hate it when the solution to a problem is a one liner.

*: This sort of insight usually doesn't come to me at the end of a long day of hard work, but instead in bed, in the shower, or, in this case, on the Lufthansa flight to Amsterdam while gazing out of the window at all the pretty lights below.


Add new comment