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
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

Incredible Demos

  • By
    Animated AJAX Record Deletion Using Dojo

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with Dojo JavaScript. The PHP - Content & Header The following snippet goes at the...

  • By
    DWRequest: MooTools 1.2 AJAX Listener & Message Display

    Though MooTools 1.2 is in its second beta stage, its basic syntax and theory changes have been hashed out. The JavaScript library continues to improve and become more flexible. Fellow DZone Zone Leader Boyan Kostadinov wrote a very useful article detailing how you can add a...

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!