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

moquirejs

v0.3.1

Published

mock require dependencies

Downloads

13

Readme

#Moquire

##mock the dependencies of AMD modules when testing

This is a simple JavaScript project for mocking AMD modules, which is useful when testing the functionality and behaviour of individual modules. Moquire is designed to work like the require method provided by require.js, taking an optional first argument which is an object of mocked modules.

Below are some example use cases. Have a look at the tests directory on how to set everything up to work for a large project.

###Function exactly the same as require

The following shows how to use moquire as a drop in replacement for require.

moquire(["dependency1", "dependency2"], function(dep1, dep2){
  //do something with dep1 and dep2
});

###Replace dependency with custom module

Simple modules which don't have any dependencies can be mocked inline:

//This module abstracts away the ajaxLib from the main application
define("backend", ["ajaxLib"], function(ajaxLib){
  return{
    sendToServer: function(data, callback){
      return ajaxLib.post("/ajax/postData", data, callback);
    }
  };
});

//This module lets you increment a value and send the result to the server
define("businessLogic1", ["backend"], function(backend){
  return {
    value: 0,
    incrementValue: function(){
      this.value++;
      //it uses the backend module to send data to the server
      backend.sendToServer(this.value+1, function(){
        console.log("success");
      });
    }
  };
});

moquire({
  //define a mock of backend which doesn't contact the server 
  //(no need to contact the server during desting)
  "backend":function(){
    return {
      sendToServer: function(data, callback){
        callback({success: true});
        return true;
      }
    };
  }
},["businessLogic"], function(businessLogic){
  //businessLogic has a dependency to backend, which has now been replaced
  //with the mock object defined above. 
});

###Replace dependency with another module

You can place the mock module in another file, and require will find it for you

//This could be in another file, named backendMock.js
define("backendMock", [], function(){
  return{
    sendToServer: function(data, callback){
      callback({success: true});
      return true;
    }
  };
});

moquire({ "backend":"backendMock"},["businessLogic"], function(businessLogic){
  //businessLogic has a dependency to backend, which has now been replaced
  //with backendMock.
});

###Replace dependency for only some objects

You can place the mock module in another file, and require will find it for you

moquire({
  "businessLogic1": {
    "backend":"backendMock"
  }
},["businessLogic1", "businessLogic2"], function(businessLogic1, businessLogic2){
  //Only businessLogic1 will have it's backend module replaced with backendMock. 
  //businessLogic2 will have the original backend module
});

###Configuration

For the mocking to work correctly, moquire needs the same configuration as require. The best way to ensure this is to configure everything the following way:

require.config(moquire.config({
    //The usual requireJS configuration
}));