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

mock.js

v0.2.0

Published

Lightweight Mock Object Testing For Javascript.

Downloads

33,103

Readme

Mock.js Testing Utility

Master: Build Status

Mock.js is a Javascript resource library that allows the user to 'fake' certain Javascript objects in order to test their functionality properly by assessing the flow of a Javascript function execution.

Table of Contents

  • Instantiate Mock.js
  • Testing using called()
  • Testing using calledWith()
  • Testing using returnedWith()
  • Spy/Mock global methods
  • Releasing Mocks
  • Integration with a UnitTest Framework
  • License
  1. Instantiate Mock.js

Mock.js hooks into an object like a spy and monitors certain assertions on the method:

var mocked = Mock.Spy(new Object, 'method1');

This will create a spy on the 'Object' class for 'method1'.

  1. Testing using called()

The function called() is used to test the amount of times a certain method is being called during a Javascript function execution.

We start out by creating our mocked object and methods and then calling the specific method we want to test.

var object = new Object()

var mocked = Mock.Spy(object, 'method1');

object.method1()

In order to test the amount of times a certain function has been called, you can use mock_called as follows:

mocked.called()

At this point, called() will return 1 (the amount of times the method 'method1' has been called)

  1. Testing using calledWith()

Sometimes we need to test the arguments a certain function is called with. We can accomplish this by using the calledWith() function.

Again, we create our mocked object.

var object = new Object()

var mocked = Mock.Spy(object, 'method1');

But this time, we call 'method1' with some arguments.

object.method1(this, 'hello!')

At this point, the assumption is that 'method1' was called with an 'Object' and 'string' as it's parameters...now let us test it.

mocked.calledWith()

We expect this call to return [this, 'hello1']. We can then use an absolute comparison (===) to compare the argument arrays.

  1. Testing using returnedWith()

Similar to calledWith(), we might like to sometimes check if a function returned an expected result. We can achieve this using returnedWith()

Again, we create our mocked object...but this time we give the method a response.

var object = new Object()

object.method1 = function() { return 'hello'; }

var mocked = Mock.Spy(object, 'method1');

Let's call the method and see if returned what it should.

object.method1()

We can now use the mocked object to assert the response of this method.

mocked.returnedWith()

We expect this call to return 'hello'. You might think that you don't have to even use a mock to determine this and can just call the function directly, but sometimes it's not possible to call functions directly...and then this function is very handy.

  1. Spy/Mock global methods

Sometimes you need to spy on or overwrite methods such as jQuery.ajax, you can achieve this using a spy or consume(). In order to check if a global method was called, we can use a normal spy.

var spy = Mock.Spy(jQuery, "ajax");

jQuery.ajax({"url": "example.js"});

alert(spy.called());

This should output '1' in your browser, since the global method was called once. Now if we want to completely stop ajax requests while testing, but still test the function...we can utilize consume().

var spy = Mock.Spy(jQuery, "ajax");

spy.consume('ajax result');

jQuery.ajax({"url": "example.js"});

alert(spy.called());

alert(spy.returnedWith());

Calling consume(result) means that a method will now return the specified result instead of completing it's normal flow. In this case, the method was called once and it returnedWith 'ajax result' instead of trying to load example.js.

There are some cases where returning a predefined mock result isn't the best way to write your test. In these scenarios, you can use consumeWithFake() instead to pass an executable method into the spy.

var spy = Mock.Spy(jQuery, "ajax");

spy.consumeWithFake(function (opts) {
  // do something with opts, then return a result
});

jQuery.ajax({"url": "example.js"});

alert(spy.called());

alert(spy.returnedWith());

Calling consumeWithFake() means that the method's arguments will now be passed to the mocked method, and returnedWith() will be that method's return value.

NOTE: Unfortunately using `consume/consumeWithFake` only works on synchronous ajax request for now.

6. Releasing Mocks
------------------

It is good practice to `release()` mocks when you are done with them. If you don't release mocks and spies, they will sit around silently keeping track
of method calls and potentially skewing results. Here is an example of a unreleased spy that will cause your test to fail.

```javascript
var spy = Mock.Spy(jQuery, "ajax");

spy.consume('ajax result');

jQuery.ajax({"url": "example.js"});

This overrides the jQuery.ajax functions default behaviour, but if you then need the use jQuery.ajax generic functionality...it won't work.

jQuery.ajax({"url": "example.js"}).done(function() { alert("done"); });

This will fail, since the result you specified for jQuery.ajax is no longer an instance of the object, but instead a string. To stop this, you can simply call spy.release()...like this.

var spy = Mock.Spy(jQuery, "ajax");

spy.consume('ajax result');

jQuery.ajax({"url": "example.js"});

spy.release();

jQuery.ajax({"url": "example.js"}).done(function() { alert("done"); });

Spies are automatically released in QUnit tests.

  1. Integration with a UnitTest Framework

You can refer to the 'example/' directory for a simple integration with Javascript QUnit.

  1. License

  • Copyright© 2012 Chris Brand
  • GNU General Public License
  • see http://www.gnu.org/licenses