Mock RequireJS define dependencies with config.map

I had a module dependency, that I’m pulling down with RequireJS that I needed to use and write tests against. In this case, I don’t care about the actual implementation of the module (it’s simple enough that I’m just avoiding some AJAX calls).

EDIT: make sure you look at the bottom example after the edit before using the config.map approach. I found that there is an easier way.

I did not want to change the constructor of the consumer as I had a chain of changes that would have to be made and that would have been to invasive for this task.

I found a question on StackOverflow with a short, but helpful answer from “Artem Oboturov”. We can use the config.map from RequireJs to achieve this.

Here is some code:

A module example (“usefulModule” in Common/Modules/usefulModule.js):

define([], function { "use strict"; var testMethod = function {... }; // add more functionality of the module return { testMethod; } });

A consumer of usefulModule example:

define([ "Commmon/Modules/usefulModule" ], function(usefulModule) { "use strict"; var consumerModule = function{ var self = this; // add functionality of the module } });

Using config.map in the html of the test runner page (and in your Karma config –> ):

map: {'*': { // replace usefulModule with a mock 'Common/Modules/usefulModule': '/Tests/Specs/Common/usefulModuleMock.js' } }

With the new mapping, Require will load usefulModuleMock.js from Tests/Specs/Common instead of the real implementation.

Some of the answers on StackOverflow mentioned Squire.js, which looked interesting, but I wasn’t ready to introduce a new library at this time.

That’s all you need to be able to mock a depency in RequireJS. However, there are many good cases when you should pass it in through the constructor instead of this approach.

EDIT: After all that, here’s another, probably better way:

The consumer class, updated:

define([ "Commmon/Modules/usefulModule" ], function(UsefulModule) { "use strict"; var consumerModule = function{ var self = this; self.usefulModule = new UsefulModule; // add functionality of the module } });

Jasmine test:

define([ "consumerModule", "/UnitTests/Specs/Common/Mocks/usefulModuleMock.js" ], function(consumerModule, UsefulModuleMock){

describe("when mocking out the module", function{ it("should probably just override the property", function{ var consumer = new consumerModule; consumer.usefulModule = new UsefulModuleMock; }); }); });

Thanks for letting me think out loud:-).

This article is part of the GWB Archives. Original Author: Aligned

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.