unit testing blogs

The platform that this blog utilizes is ghost, a really simple an bare-bones blog. The main reason for this publishing engine over the pletera of choices out there... Markdown. I get to write everything in markdown, and ghost will go about making it pretty. Especially if you throw some prismjs in for code highlighting. But is pretty code enough???

Rhetorically answering this with a resounding NO... the code also needs to be correct. And how do we as software engineers make sure code is correct?


That's right, unit tests. Which got me thinking a bit... Given I am already in a javascript runtime, can't I just inline my unit tests right into the blog it self? This post is a biproduct of the a couple of nights of tinkering with jasmine, iframes and events between different doms.

The two tests hopefully passing tests above are testing out the function in the code block bellow. The code, a simple add one function, is evaluated with in a iframe runtime.

function add1(num) {  
    return num + 1;

Just like the function above, the unit test logic bellow is also identified and evulated with in the iframe runtime.

describe("Running a sample code UnitTest", function() {  
  it("adds 1 to a number", function() { 

  it("adds 1 to another number", function() {

The codebase is effectivly creating new script tags inside the iframe and loading the text of the scripts into them. With the help of some promises, we can extend the logic to also sideload serverside files into the runtime. In the example bellow I am loading some scripts from my github repository and displaying the results of the run.




Be warned, this code is alpha and in some places might require some tweeks to get it to work. The path to your dependencies for example. Also, becuse i am creating the html for the iframe on the fly, you might find some strange cross domain exeptions if URLs are not formated correctly.