Loading Static Templates for Intern Testing

By  on  

I use Intern by SitePen for all of my JavaScript functional testing.  Intern has loads of features other functional test frameworks don't and it's completely Promise-based -- something I got very used to when I used the Dojo Toolkit every day. Async test creation can be difficult but I find it very rewarding.

While writing destructive tests for the Mozilla Developer Network, I decided I wanted to load specific content to test.  The first step is adding a new file with the sample content alongside my tests, but then I needed to know how to load that content alongside the other test dependencies.  Since that file isn't a JavaScript object, we need to pull that dependency in a special way:

define([
'intern!object',
'intern/chai!assert',
'intern/dojo/text!tests/fixtures/in-content.html'
], function(registerSuite, assert, contentTemplate) {
	//  Use the contentTemplate string here...
});

Adding intern/dojo/text! before the file path allows the file to be loaded without being evaluated as JavaScript.  You can  prepend that string to load any file type and use it as text in the callback!

Recent Features

  • By
    5 HTML5 APIs You Didn’t Know Existed

    When you say or read "HTML5", you half expect exotic dancers and unicorns to walk into the room to the tune of "I'm Sexy and I Know It."  Can you blame us though?  We watched the fundamental APIs stagnate for so long that a basic feature...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!