Skip to content

Instantly share code, notes, and snippets.

@burt202
Created March 3, 2014 21:05
Show Gist options
  • Save burt202/9334578 to your computer and use it in GitHub Desktop.
Save burt202/9334578 to your computer and use it in GitHub Desktop.
NodeJS / RequireJS / Jasmine

Node / RequireJS / Jasmine

A really simple example of running jasmine tests using RequireJS in Node as I couldn't find any when I was trying to work it out. It uses some external libraries just as an example but these are not necessary to get it all working.

Setup

  • copy the gist code into a directory
  • download jquery, underscore, backbone, copy them into the same directory and name them jquery.js, underscore.js and backbone.js
  • install requirejs, jasmine-node and jsdom globally via npm
  • run jasmine-node foo-spec.js to see the magic
var requirejs = require("requirejs");
require("./init");
var foo = requirejs("foo");
describe("test", function () {
it("test", function () {
expect(foo.message).toEqual("working");
});
});
define([
"underscore",
"jquery",
"backbone"
], function (_, $, Backbone) {
var model = new Backbone.Model({
tester: "abc123"
});
return {
message: "working",
number: _.random(0, 100),
version: $.fn.jquery,
model: model.toJSON()
};
});
var jsdom = require("jsdom"),
requirejs = require("requirejs");
global.document = jsdom.jsdom("<html><body></body></html>");
global.window = global.document.parentWindow;
requirejs.config({
baseUrl: __dirname
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment