Access Native Node.js Modules within Intern

By  on  

Intern is an awesome unit and functional test suite from SitePen.  I've been using this utility extensively over the past year, writing tests to make sure MDN's front-end is in good shape after code merges and pushes.

Sometimes when writing tests I'll want to make use of a node module to accomplish a test task, like making an HTTP request or getting environment information.  It isn't as easy as making the same dependency path you would if you were writing a node module -- you're using the Dojo loader so you'll need to require those modules a bit differently:

define([
	'intern/dojo/node!http',
	'intern/dojo/node!process'
], function(http, process) { 

	// http and process now available from the Node.js environment

});

The dojo/node Dojo module gives your Intern test suite the ability to access native Node.js modules!

Recent Features

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

Incredible Demos

  • By
    Introducing MooTools LazyLoad

    Once concept I'm very fond of is lazy loading. Lazy loading defers the loading of resources (usually images) until they are needed. Why load stuff you never need if you can prevent it, right? I've created LazyLoad, a customizable MooTools plugin that...

  • By
    Fade Images with MooTools LazyLoad

    I recently received an email from a MooTools developer asking a great question about my LazyLoad class: "I'm using your LazyLoad MooTools plugin (which is great, by the way). I have been trying to figure out how to modify it so that once an image scrolls into...

Discussion

  1. Seems like it’d be a lot easier to use the default require/exports/module style, so that your test module feels a lot more like Node. The above code could be written like so:

    define(function (require) {
      var http = require('intern/dojo/node!http');
      var process = require('intern/dojo/node!process');  
    });
    

    Still unfortunate that you have to go through intern/dojo/node but it works.

    It’s also in line with the conventions as prescribed in the Intern user guide at https://theintern.github.io/intern/#testing-commonjs-code

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