Wednesday, February 12, 2014

Auto-running a Bunch of Polymer Tests


Mea culpa, mea culpa, mea maxima culpa.

Forgive me for I have not been testing. It has been two and a half long months since I last tested a Polymer. For penance I must figure out how to test all of these Polymers in one night.

Honestly, I do not need much. A simple test or two for each Polymer used in Patterns in Polymer would likely be enough. It would be nice if the tests upgraded Polymer on each run so that I might find out sooner rather than later when a new version of Polymer breaks things. Then a smoke test or two to verify that the element is working like I expect / hope.

Upgrading seems easier than I expected. At least easier in JavaScript thanks to Bower. I already know that Dart handles this out of the box with pub upgrade. I was not sure about Bower until I tried it:
➜  js git:(master) bower update
bower polymer#~0.1.1            cached git://github.com/Polymer/polymer.git#0.1.4
bower polymer#~0.1.1          validate 0.1.4 against git://github.com/Polymer/polymer.git#~0.1.1
bower platform#0.1.4            cached git://github.com/Polymer/platform.git#0.1.4
bower platform#0.1.4          validate 0.1.4 against git://github.com/Polymer/platform.git#0.1.4
bower polymer#~0.1.1           install polymer#0.1.4
bower platform#0.1.4           install platform#0.1.4

polymer#0.1.4 bower_components/polymer
└── platform#0.1.4

platform#0.1.4 bower_components/platform
A simple bower update before each test run ought to do nicely. Building on that knowledge and with a 2.5 month old Karma test, I start a run-all-the-tests script. I'll get to all-the-tests in a bit, but start with just a single pass to make sure I've got this in order. The bash script that I use is:
#!/bin/bash

# Change the current working directory to a chapter's code directory:
cd book/code-js/svg

# Update bower
bower update

# Run the Karma tests
karma start --single-run

# Handle failure
if [[ $? -ne 0 ]]; then
    echo "Some tests failed."
    exit 1
fi

# Success!
echo "Success!"
Amazingly, that just works:
➜  polymer-book git:(master) ✗ ./scripts/test.sh
bower polymer#~0.1.3            cached git://github.com/components/polymer.git#0.1.4
bower polymer#~0.1.3          validate 0.1.4 against git://github.com/components/polymer.git#~0.1.3
bower platform#0.1.4            cached git://github.com/Polymer/platform.git#0.1.4
bower platform#0.1.4          validate 0.1.4 against git://github.com/Polymer/platform.git#0.1.4
INFO [karma]: Karma v0.10.4 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 34.0.1825 (Linux)]: Connected on socket JY9_C91o2eqYwtV6hFYk
Chrome 34.0.1825 (Linux): Executed 1 of 1 SUCCESS (0.567 secs / 0.477 secs)
Success!
The only drawback to this approach is that I need a actual Chrome window running. Continuous integration might be easier with something like PhantomJS.

Unfortunately, after I enable it in Karma, I get:
PhantomJS 1.9.7 (Linux) ERROR
        ReferenceError: Can't find variable: Window
        at /home/chris/repos/polymer-book/play/svg/js/bower_components/platform/platform.js:30
PhantomJS 1.9.7 (Linux): Executed 0 of 1 ERROR (0.174 secs / 0 secs)
Try as I might, I cannot seem to get rid of this. Defining a Window function in the test only results in a different error:
PhantomJS 1.9.7 (Linux) ERROR
        TypeError: 'undefined' is not an object (evaluating 'a.prototype')
        at /home/chris/repos/polymer-book/book/code-js/svg/bower_components/platform/platform.js:29
PhantomJS 1.9.7 (Linux): Executed 0 of 1 ERROR (0.137 secs / 0 secs)
It seems unlikely that I can figure that out without installing the non-minified version of the Polymer platform. That seems too big a yak to shave—at least for now.

Happy with the start of the script, I convert it to iterate over all of the book directories and get work writing a some good tests.

Mea cupla.


Day #1,025

No comments:

Post a Comment