Skip to content

sjhewitt/requirejs-isolate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

require-isolate

require-isolate is a plugin to requirejs that helps simplify testing modules you have defined by providing a configurable isolation layer around your modules. The isolation layer can be configured to behave as an automocking container.

Usage: as a require.js plugin

define(["isolate!myModule", anotherModule], function(myModule, anotherModule){
  // ...
});

myModule will be an isolated instance of your module. Any dependencies it required have been replaced by stubs.

anotherModule will be a normal, non-isolated instance of your module. If anotherModule itself depends on myModule, it will get a normal, non-isolated instance of myModule as it's dependency.

Usage: as a direct api

define(["isolate"], function(isolate){
  isolate.load("myModule", function(myModule){
    // ...
  });
});

A manual call to isolate.load has the same effect on the target module instance as using isolate as a require.js plugin.

Creating new IsolationContexts

New contexts can be spawned and separately configured using the .createContext() method.

 define(["isolate"], function(isolate){
   isolate.createContext()
    //... calls to .configure or .load can follow
 });

Context Configuration: runtime configuration

Using the direct api, you can configure your isolation context.

define(["isolate"], function(isolate){
  isolate.configure( function(ctx){
     // configuration here
  }).load("myModule", function(myModule){
    // ...
  });
});

Very Important! Note here .configure does not return a new IsolationContext instance. Calls to .configure modify the configuration of the context on which it is called. To spawn a new IsolationContext for configuration, use .createContext().configure instead.

Chaining Methods

.configure(...) and .createContext() return an IsolationContext instance, so they can be chained together for easier reading.

define(["isolate"], function(isolate){
 isolate
   .createContext()
   .configure( function(ctx){
      /* configuration here */ })
   .load("myModule", function(myModule){
     /* ... */ });
});

Context Configration: API

Within either configuration option mentioned above, these methods are available to you for configuring the context:

passthru()

passthru takes a params array of module names for which it should simply provide the real implementations for when requested as a dependency.

isolate.configure( function(ctx){
  ctx.passthru("myFirstModule", "mySecondModule");
});

This setting cascades to any further spawned contexts.

map(moduleName, implementation)

map specifies that the given implementation should be provided for any dependencies of moduleName.

var wasCalled = false;
isolate.configure( function(ctx){
  ctx.map("myFirstModule", function(){ wasCalled = true; });
});

mapType(typeName, factory)

mapType configures isolate to consult the provided factory method when encountering an implementation of type typeName to mimic. The types currently supported are function and object.

The factory method is passed two parameters: the real module implementation and the current IsolationContext configuration. This second parameter makes it possible to consult factories for other types from within your provided factory implementation.

isolate.configure( function(ctx){
  ctx.mapType("function", function(original, contextConfiguration){ return some_cool_transformation_of(original); });
});

Also, each of these configuration methods returns the configuration api object, so they can be chained for easy reading and less typing:

isolate.configure( function(ctx){
  ctx.passthru("one","two","three")
     .mapType("object", function(){ return {} })
     .map("a", { })
     .map("b", { });
});

About

Automocking / Module Isolation plugin for require.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published