npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

requirejs-mock

v1.0.1

Published

Dependency injector for RequireJS modules.

Downloads

20

Readme

Requirejs-mock Build Status Coverage Status

About

requirejs-mock is a dependency injector for RequireJS modules. It allows you easily mock your module dependencies.

Installation

npm install requirejs-mock --save-dev

Usage example

var requirejs = require('requirejs');
var Injector = require('requirejs-mock').provide(requirejs);

describe('Test', function() {
  var injector;
  var mockA;
  var mockB;

  beforeEach(function() {
    // instantiate injector
    injector = Injector.create();

    // remapping to mock module (defined in separate file)
    injector.map('module/a', 'mock/a');

    // if you need to check smth on mockA
    mockA = injector.require('module/a');

    // use direct mock
    mockB = { someAction: jasmine.createSpy() };
    injector.mock('module/b', mockB);
  });

  // cleanup
  afterEach(function() {
    injector.destroy();
  });

  it('should work', function() {
    // require module C that depends on A and B
    var c = injector.require('module/c');
    c.doSmth();

    expect(mockB.someAction).toHaveBeenCalled();
    expect(mockA.someAction).toHaveBeenCalled();
  });
});

API

Instantiate injector

First of all you need to create an injector that will allow you to mock your modules dependencies.

var requirejs = require('requirejs');

// Get Injector and provide requirejs to it
var Injector = require('requirejs-mock').provide(requirejs);

beforeEach(function() {
  // instantiate injector (default context)
  var injector = Injector.create();

  // instantiate injector for non-default context
  var otherInjector = Injector.create({ context: 'specs' });
});

Maps

Setup maps

If you have a module mock in a separate file and just need to replace module with it - you should use module mapping.

beforeEach(function() {
  var injector = Injector.create();

  // remap module A to its mock
  injector.map('module/a', 'mock/a');
  injector.map('module/b', 'mock/b');

  // or as an object
  injector.map({
    'module/a': 'mock/a',
    'module/b': 'mock/b'
  });

  // load module C (that depends on A and B)
  var c = injector.require('module/c');
});

You could also provide different maps at the same time and restore original module value:

beforeEach(function() {
  var injector = Injector.create();

  // get original module value
  var aOrig = injector.require('module/a');

  // remap module/a => mock/a
  injector.map('module/a', 'mock/a');

  var a = injector.require('module/a');
  console.log(a === aOrig); // => false

  // remap module A => mock/a_one
  injector.map('module/a', 'mock/a_one');

  var aOne = injector.require('module/a');
  console.log(aOne === a); // => false
  console.log(aOne === aOrig); // => false

  // restore original module
  injector.unmap('module/a');
  console.log(
    injector.require('module/a') === aOrig
  ); // => true
});

Cleanup maps

If you need to get an original module instead of mapped mock - you could unmap the module.

// unmap single module
injector.unmap('module/a');

// unmap few modules at once
injector.unmap('module/a', 'module/b');

// unmap all modules mapped before
injector.unmap();

Notice: It's not possible to use injector.mock() and injector.map() for module at the same time.

Mocks

Setup mocks

If you do not have a prepared module mock in a separate file and want to do it in the runtime - you could use mocks. It's possible to provide any value (function, object, number, string, etc) as a result for the mocked module.

describe('Mocks', function() {
  beforeEach(function() {
    // provide a value that will be returned as a module
    injector.mock('module/a', valueA);
    injector.mock('module/b', valueB);

    injector.mock({
      'module/a': valueA,
      'module/b': valueB
    });
  });

  it('should work', function() {
    expect(injector.require('module/a')).toBe(valueA);
    expect(injector.require('module/b')).toBe(valueB);
  });
});

Notice: It's not possible to use injector.mock() and injector.map() for module at the same time. Notice: It's not possible to mock module that has been mocked before.

Cleanup mocks

If you don't need a mock anymore it's possible to remove it. Afterwards original module value will be returned if you require the module.

// Unmock single module
injector.unmock('module/a');

// Unmock few modules at once
injector.unmock('module/a', 'module/b');

// Unmock all previiously mocked modules
injector.unmock();

Undefining a module

When you require a module - RequireJS will automatically cache it with all dependencies. If you need to provide another dependency mocks you should undefine module and then get fresh copy of module.

// Assume that you have module C that depends on module A and B
// module C is required and cached
var c = injector.require('module/c');

// provide mocks for dependencies
injector.mock('module/a', 123);
injector.mock('module/b', 456);

// undefine module C to get fresh dependencies afterwards
injector.undef('module/c');

// get module C with mocked dependencies
c = injector.require('module/c');

Notice: During the mock setup - module cache will be removed automatically for you and mocked module value will be returned afterwards, as expected.

// get original A value
var a = injector.require('module/a');

// mock module A
injector.mock('module/a', 123);
injector.require('module/a'); // => 123

Destroying the injector

You should always destroy injector to cleanup after it.

afterEach(function() {
  // cleanup
  injector.destroy();
});

RequireJS versions support

requirejs-mock supports RequireJS versions starting from 2.1.12. Previous versions are not supported.

Changelog

1.0.0 - Mar. 22, 2015

First stable release that supports all planned injector features.

  • [breaking] It's not possible to mock() and map() module at the same time. Previous behavior was not specified
  • Added ability to remove mocks and maps used before
  • Added ability to remove module from RequireJS cache

0.9.0 - Mar. 16, 2015

Initial release with basic capabilities