Chapter 12. jQuery Plugins

Mike Hostetler

Introduction

A primary goal of the jQuery JavaScript library is to remain a fast and concise alternative to other JavaScript libraries that are available in the open source world. A key principle toward this goal is ensuring that the jQuery core addresses the needs of most developers, while remaining fast and concise. Developers may have needs that aren’t completely satisfied by the jQuery core. Or, a developer may write an extension to core jQuery functionality that may be useful to a significant segment of jQuery users but shouldn’t be included in the jQuery core.

jQuery was designed to be extensible in a variety of ways. The recipes in this chapter are intended to introduce the reader to the world of jQuery plugins.

12.1. Where Do You Find jQuery Plugins?

Problem

You’re trying to build something with jQuery that requires functionality that doesn’t exist in the jQuery core. The problem is one that other developers have likely run into before, and you think a plugin may exist. Where should you start looking to find plugins, and how should you evaluate the plugins that you find?

Solution

Search through the following repositories for jQuery plugins:

jQuery Plugin Repository

http://plugins.jquery.com

Google Code

http://code.google.com

GitHub

http://github.com

Google with special queries

http://google.com

SourceForge

http://sourceforge.net

Discussion

There are a few places around the Web that jQuery plugins may be found. Because of the nature of jQuery plugins, there are certain open source hosting sites that tend to attract jQuery plugins more than others. Additionally, the jQuery project hosts a central repository for jQuery plugins at http://plugins.jquery.com.

It’s best to look through all of the available resources and collect several potential plugins, if they are available, for your review. Plugins that are built to solve the same problem often take very different approaches or were built for alternate versions of the jQuery core library.

When looking for a jQuery plugin, the following are the best steps to find the most updated and recent versions of plugins.

Search through the jQuery Plugin Repository

The jQuery Project hosts a plugin repository that currently boasts more than 1,200 plugins at the time of this writing. Most authors who host their own plugins will post their plugins here.

Plugins hosted in the jQuery Plugin Repository are organized into a number of categories, which can assist with narrowing your search. Plugins may be organized into multiple categories. Plugins are also required to be listed by API compatibility, ensuring that any plugins you find are likely to work with a particular version of the jQuery core library. Lastly, you may also browse plugins by release date, allowing you to keep up with your favorite plugins as new versions are released.

Search through Google Code

Google Code hosting offers a very rich repository of jQuery plugins. More often than not, if you can’t find a plugin hosted on the main Plugin Repository, there’s a good chance it could be on Google Code.

Search through GitHub

GitHub is a rising star in the code hosting world that many jQuery plugin authors are turning toward. More and more plugins end up here, and it is certainly a site that warrants a search when looking for a specific plugin. One of the most compelling features of GitHub is the ability to “fork” a repository in a friendly way by utilizing the features of the Git source code management system. In the event that you need to modify an existing plugin, utilizing the features of GitHub are a compelling way to keep track with upstream updates.

The best way to find a plugin on GitHub is to utilize GitHub’s excellent search. GitHub supports a number of advanced operators when searching. All of these options may be viewed in greater detail at http://github.com/search. When looking specifically for a jQuery plugin, searching for repositories using JavaScript will return the best results.

Perform a Google search

While the previous suggestions are known sources of plugins, searching throughout the entire Web via Google is useful as well. Because the body of search material tends to be larger, so does the number of potential results to sift through. Using a few of the suggested searches can result in finding plugins quicker:

{searchterm} "jquery*.js" - Best practice plugin naming is jquery-{myplugin}.js or 
jquery.{myplugin}.js

 {searchterm} "*jquery.js" - Alternate best practice plugin naming

Search through SourceForge

There tends to be very few actual jQuery plugins hosted on SourceForge. However, a number of projects on this site offer jQuery support tools, such as IDE code completion extensions. If you’re out of options, or are looking for something unique, SourceForge is a good place to do a quick search.

12.2. When Should You Write a jQuery Plugin?

Problem

After searching for an existing jQuery plugin to fit your needs, the plugins that were found either don’t meet your needs or are not constructed in a way that you can take advantage of them properly. Is it worth writing a new jQuery plugin that can be shared with others who have the same need?

Solution

There’s no cut and dried solution to this problem. The number of available jQuery plugins is large, but there are valid cases where plugins don’t exist to meet a particular need.

In my opinion, the decision to write and publish your own jQuery plugin comes down to three things:

  • Is it likely there are others who have the same problem?

  • What level of support are you willing to provide?

  • What level of community participation do you desire?

Discussion

Build a plugin if there is a potential audience

If you’re facing a problem that remains unsolved, there are likely other developers who have encountered the same issue. How others before you have solved the issue is the key question. It’s assumed that you’ve done some homework at this point, searching for a solution. During that search, clues that may surface that point toward a need for a plugin can be found in forum posts or mailing list questions that have gone unanswered. There’s no easy way to decide whether a plugin is worth building, and the decision ultimately comes down to the person who is planning to build the plugin. However, a general feel of whether there is a potential audience is worth exploring.

The other potential reason to build and publish your own plugin is if a plugin exists to meet your needs but does not fully do what you want. If this is the case, it is worth considering the potential for writing a patch and submitting that patch back to the original author for inclusion in the plugin. Participating in the open source process by submitting a patch to an existing project tends to be a much more efficient application of a developer’s most precious resource: time.

Know and communicate the level of support you are willing to provide

If writing your own plugin is the best option, a bit of forethought and planning will help make sure the process of hanging out your own open source shingle goes well.

Whenever you decide to publish your code, the first and biggest consideration is licensing. The jQuery core project is dual-licensed as MIT and GPL, but many other open source licenses are worthy of consideration. A more thorough discussion on the intricacies of open source licensing can be found at Wikipedia.

Second, it is important to consider and communicate the level of support that you, the plugin author, are willing to provide to others who may download and use your code. Choosing to simply publish your code and provide no support is a completely valid option and is much better than keeping your code to yourself for fear of the potential support issues. The key is communication; writing a quick note about your support plan into the comments of your plugin will go a long way.

If you are willing to provide deeper support for a plugin that you want to publish, there are several great source code hosting sites that offer several features to assist in supporting your plugin. See Recipe 12.1 for a list of the best places to host and support your plugin.

Plan for participation from others

Lastly, think through and gauge your willingness to accept participation from others. Participation is a key component of the open source ecosystem, and it is wise to communicate your intention from the moment you publish your plugin. The attraction of allowing participation is that you can benefit from the work of others. Plugins that accept participation from others tend to attract additional users, partly because of the appearance of activity and partly because active code tends to be more trustworthy code.

Communicating the path to participation is key. Whether you intend to or not, any piece of code that is published tends to attract some sort of participation once users find it. Having a plan to engage that participation in an open and public way is essential.

One last word of advice: engaging participation simply by publishing your email address and allowing people to email you with comments and questions is generally a bad idea for a couple reasons. First, email isn’t a public forum that displays activity to potential users, and second, it introduces you, the plugin author, as a bottleneck to integrating that activity back into the plugin.

12.3. Writing Your First jQuery Plugin

Problem

You’ve decided that you want to write a jQuery plugin. How do you write a plugin in jQuery? What best practices should you follow?

Solution

jQuery is designed to make writing a plugin very simple and straightforward. You can extend the existing jQuery object by writing either methods or functions. Simply declaring the following JavaScript after inclusion of the jQuery core library will allow your code to use your new custom method or function.

Writing a custom jQuery method

jQuery methods are available to be chained and thus can take advantage of jQuery selectors. jQuery methods are defined by extending the jQuery.fn object with your method name. Because the jQuery object must be able to handle multiple results, you must wrap your custom functionality inside a call to the each() function to apply your code to all of the results:

jQuery.fn.goShop = function() {
  return this.each(function() {
    jQuery('body').append('<div>Purchase: ' + this.innerHTML + '</div>'),
  });
};

Accessing this new plugin is as simple as calling jQuery like you normally would and utilizing your new method name:

jQuery('p').goShop();

Writing a custom jQuery function

Functions are attached to the main jQuery object. Functions are designed to be called outside of a jQuery selection:

jQuery.checkout = function() {
  jQuery('body').append('<h1>Checkout Successful</h1>'),
};

This new function can be manipulated and called normally:

jQuery.checkout();

Discussion

Attaching new methods and functions to the main jQuery object are a powerful feature of jQuery. Many of the core methods are built into the library using this same technique. By leveraging this existing foundation in jQuery, users of jQuery and users of your plugin have a fast and concise way to add new functionality, extend existing functionality, and mold the jQuery code into whatever form suits them best. This flexibility is a key feature and enables jQuery and its plugins to be used by a wider audience.

The choice to extend jQuery via a new method or a function mainly depends on the needs of the developer. In general, focusing on extending jQuery via adding a new method is best because this allows that new method to be chained along with other methods, and it allows the code in the method to take advantage of jQuery’s selector engine.

12.4. Passing Options into Your Plugin

Problem

Your first plugin adds a method to jQuery. However, there are a few options that would be helpful to others if they were exposed properly. What is the best method of passing options into a custom method?

Solution

Options are best passed into your custom plugin method via an options object. Using a single options object to pass in parameters promotes cleaner code, is easier to work with, and provides flexibility down the road.

When allowing options to be utilized in your plugin, it’s wise to provide sensible defaults. After providing sensible default options, it’s also important that your plugin provide a method for the user of the plugin to override the defaults. Both of these goals are easily accomplished by declaring a default options object, overriding the default options with user-supplied options and the jQuery extend() method, and then utilizing the options in your code:

jQuery.fn.pulse = function(options) {
  // Merge passed options with defaults
  var opts = jQuery.extend({}, jQuery.fn.pulse.defaults, options);

  return this.each(function() {
    // Pulse!
    for(var i = 0;i<opts.pulses;i++) {
      jQuery(this).fadeTo(opts.speed,opts.fadeLow).fadeTo(opts.speed,opts.fadeHigh);
    }

    // Reset to normal
    jQuery(this).fadeTo(opts.speed,1);
  });
};

// Pulse plugin default options
jQuery.fn.pulse.defaults = {
  speed: "slow",
  pulses: 2,
  fadeLow: 0.2,
  fadeHigh: 1
};

By specifying option defaults, developers using your plugin have the ability to provide as many or as few options when they call your function. It is important to place your options’ defaults after you’ve defined your plugin entry method; otherwise, you will encounter an error:

// Override only one option
jQuery('p').pulse({pulses: 6});

// Override all options
jQuery('p').pulse({speed: "fast", pulses: 10, fadeLow: 0.3, fadeHigh: 0.8});

Lastly, by specifying your options as an object attached as a child to your plugin function, the default options may be overridden only once in a project. A developer then has the ability to specify their own set of default options, minimizing the amount of code required to produce the desired behavior:

// Plugin code included above

// Reset pulse default options
jQuery.fn.pulse.defaults = {
  speed: "fast",
  pulses: 4,
  fadeLow: 0.2,
  fadeHigh: 1
};

// This call will use the new defaults
jQuery('p').pulse();

Discussion

Supporting options in your plugin is a powerful way to add tremendous flexibility to the plugin. Plugins that support a rich set of options are more likely to fit the needs of a wider audience, perform a wider variety of tasks, and generally gain more popularity than plugins that don’t support options.

Including a set of default options with your plugin is another way to give developers who use your plugin flexibility and choice in how the plugin is implemented. A handy side benefit is that the plugin can always rely on certain options being defined, reducing the amount of code required to check whether an option has been passed. This leaves plugin users with the ability to override a single option, multiple options, or even all of the options every time they call your plugin. Lastly, by attaching the default options to the jQuery object, the options can be overridden globally, giving your users another tool to leverage in new and creative ways.

12.5. Using the $ Shortcut in Your Plugin

Problem

Other JavaScript libraries make use of the $ shortcut. jQuery itself uses $ only as a shortcut, with the main object being named jQuery. How can you ensure that your plugin maintains compatibility with other plugins and libraries?

Solution

jQuery itself uses the $ function as a custom alias for the jQuery object. When jQuery is set into compatibility mode, it passes back control of the $ alias to the original library that defined it. Plugins can be crafted to use the same technique.

By wrapping your plugin in an anonymous function and immediately executing that function, the $ shortcut is kept inside the plugin. Code outside of the plugin can use $ normally. Inside the plugin, $ will reference the jQuery object as normal:

;(function($) {
  $.fn.pulse = function(options) {
    // Merge passed options with defaults
    var opts = $.extend({}, $.fn.pulse.defaults, options);

    return this.each(function() {
      // Pulse!
      for(var i = 0;i<opts.pulses;i++) {
        $(this).fadeTo(opts.speed,opts.fadeLow).fadeTo(opts.speed,opts.fadeHigh);
      }

      // Reset to normal
      $(this).fadeTo(opts.speed,1);
    });
  };

  // Pulse plugin default options
  $.fn.pulse.defaults = {
    speed: "slow",
    pulses: 2,
    fadeLow: 0.2,
    fadeHigh: 1
  };
})(jQuery);

Discussion

Wrapping your distributed code in an anonymous function is a very straightforward and simple step that adds several features and ensures that your plugin code can play nicer in the wider world that your users may live within.

Adding a semicolon at the beginning of your function definition helps protect against another developer who may have forgotten to include an ending semicolon in their library. The JavaScript language breaks statements on newline by default, but many users take advantage of minimization tools that compress the entire set of JavaScript in their projects into a single file. This process removes the line endings and can cause errors if your code follows immediately after. Adding the initial semicolon is a quick and easy trick to protect against that possibility.

The open parenthesis immediately begins the anonymous function definition. Within our anonymous function, we define a function that passes the variable that we want to use in place of the fully named jQuery object. In this case, we want to take advantage of using $ as the variable. Defining an additional function is required because of the way the JavaScript language handles scoping. In more traditional languages such as Java and C++, scope is limited to the block statement. In JavaScript, scope is wrapped in functions. Therefore, the reason for using a function here is really to set up a scope boundary that we can define our plugin within.

What follows is a new version of our plugin, with the sole change of swapping out the way we utilize the jQuery object. Because we’ve wrapped this plugin anonymously and limited the scope of the $ variable, we can now use $ freely without conflict from any other code.

The last line wraps up the scoping function and anonymous function with a close bracket and close parenthesis, respectively. The last bit is what actually calls our anonymous function immediately after it has been defined. This is where we tell our function to pass in the jQuery object, which is what gets renamed to $ within our function. Lastly, we close off our new statement with a semicolon to protect against JavaScript minimization and compression errors.

The $ shortcut can be incredibly useful in writing JavaScript code. It cuts down on code size, promotes good code design, and has become extremely popular and well known. Thus, many libraries take advantage of the $ shortcut, tying it into their own context. With each library supporting their own version of the $ shortcut, conflicts can easily arise. By wrapping your plugin code within an anonymous function, you can ensure that your plugin maintains a level of scope around usage of the $ shortcut that will reduce the potential for conflicts with other JavaScript libraries.

One additional side effect of wrapping your plugin in an anonymous function, as described earlier, is that a closure is created. Utilizing a closure in JavaScript aids in properly namespacing any methods or variables that you may need to define, further reducing the chance for variable names or function names to conflict with other code.

12.6. Including Private Functions in Your Plugin

Problem

Your plugin code is growing and needs to be organized. How can you implement a private method that’s unavailable to code outside your plugin?

Solution

By utilizing the plugin design pattern started in Recipe 12.4, private functions may be defined normally within the anonymous function that we’ve wrapped our plugin in. Because the function is enclosed in an anonymous function, outside code won’t see our private method. Code outside will only be able to see functions or methods that are attached to the jQuery object.

;(function($) {

  $.fn.pulse = function(options) {

    // Merge passed options with defaults
    var opts = $.extend({}, $.fn.pulse.defaults, options);

    return this.each(function() {
      doPulse($(this),opts);
    });
  };

  function doPulse($obj,opts) {
    for(var i = 0;i<opts.pulses;i++) {
      $obj.fadeTo(opts.speed,opts.fadeLow).fadeTo(opts.speed,opts.fadeHigh);
    }

    // Reset to normal
    $obj.fadeTo(opts.speed,1);
  }

  // Pulse plugin default options
  $.fn.pulse.defaults = {
    speed: "slow",
    pulses: 2,
    fadeLow: 0.2,
    fadeHigh: 1
  };

})(jQuery);

Discussion

Because we now have our plugin wrapped in an anonymous function, defining private functions within our plugin is as simple as adding a new function as you normally would.

Grouping and organizing your plugin with public and private methods offers many advantages to your users and to the plugin author. As your plugin matures and you receive feedback from the community, you can leverage the use of public and private methods to provide a consistent API between plugin versions. The consistency of your API can be a major factor in your plugin’s success.

The ability to break code down into private and public messages also has significant advantages in code organization as your plugin grows. Well-organized code is easier to read, to maintain, and to test. Well-tested, clean code can lead to less error-prone code.

12.7. Supporting the Metadata Plugin

Problem

Several plugins utilize the metadata plugin to pass custom options into their methods. How can integration with the metadata plugin be constructed?

Solution

Leveraging the metadata plugin is as simple as checking whether the plugin is available and then extending your plugin options with the metadata parameters. Using this technique, you can supply default options when making the call to your plugin and override those default options for each object to be operated on through the metadata written into the markup:

<!-- Include the metadata plugin -->
<script type="text/javascript" src="metadata/jquery.metadata.js"></script>

<!-- Example of markup containing metadata -->
<p class="{pulses: 8, speed: 'slow'}">Starship Enterprise</p>
<p>Battlestar Galactica</p>
<p class="{speed: 100}">Serenity</p>

;(function($) {
  $.fn.pulse = function(options) {
    // Merge passed options with defaults
    var opts = $.extend({}, $.fn.pulse.defaults, options);

    return this.each(function() {

      // Merge in the metadata elements for this specific node
      var o = $.metadata ? $.extend({}, opts, $.metadata.get(this)) : opts;

      doPulse($(this),o);
    });
  };

  function doPulse($obj,opts) {
    for(var i = 0;i<opts.pulses;i++) {
      $obj.fadeTo(opts.speed,opts.fadeLow).fadeTo(opts.speed,opts.fadeHigh);
    }

    // Reset to normal
    $obj.fadeTo(opts.speed,1);
  }

  // Pulse plugin default options
  $.fn.pulse.defaults = {
    speed: "slow",
    pulses: 2,
    fadeLow: 0.2,
    fadeHigh: 1
  };
})(jQuery);

Discussion

Including the metadata plugin is a great example of how jQuery plugins can build off of one another. The jQuery plugin ecosystem is vast, and chances are there are other plugins that you can utilize.

To include and use the metadata plugin, you first must actually include it into your script. The metadata plugin is hosted along with jQuery at Google Code. The metadata plugin works by allowing you to embed additional data into your HTML, while still producing valid HTML. We take advantage of this by allowing users to embed element-specific options into the class element of the items we can operate on.

The options are embedded into the HTML using standard JSON. All of the options may be embedded, or none may be embedded; it’s up to your users. There are several other methods and options for using the metadata plugin that are described on its documentation page.

Within our plugin, we first check to see whether a user has included the metadata plugin. This is done to ensure that we keep this additional feature optional and to provide backward compatibility, if necessary. Because the metadata plugin operates on a single element, we split up how we handle options. The first step is to use the options provided when the plugin was called. These options are extended with our default options, creating our starting point for this first instantiation of our plugin. The second step is to extend those locally default options with the metadata that may be defined for each element. All that is required is for us to extend our locally default options with the metadata options, if the metadata plugin exists.

The metadata plugin provides another option for users of your plugin to pass in options. Providing options to potential users is a great way to show that you are committed to your plugin, being a good citizen of the jQuery ecosystem. The metadata plugin is also a great way to offer your users the ability to write less code by embedding custom options into the HTML elements.

12.8. Adding a Static Function to Your Plugin

Problem

In addition to making your plugin available through the jQuery function, you want to expose a static function. How can you add a static function to your jQuery plugin?

Solution

Adding a static method to your plugin requires extending the jQuery object in much the same way you would add a method. The difference is simply that functions are called without using jQuery selectors:

;(function($) {
  $.fn.pulse = function(options) {
    // Merge passed options with defaults
    var opts = $.extend({}, $.fn.pulse.defaults, options);

    return this.each(function() {

      // Merge in the metadata elements for this specific node
      var o = $.metadata ? $.extend({}, opts, $.metadata.get(this)) : opts;

      doPulse($(this),o);
    });
  };

  function doPulse($obj,opts) {
    for(var i = 0;i<opts.pulses;i++) {
      $obj.fadeTo(opts.speed,opts.fadeLow).fadeTo(opts.speed,opts.fadeHigh);
    }

    // Reset to normal
    $obj.fadeTo(opts.speed,1);
  }

  // Define our base to add to
  $.pulse = {};

  // Static Function
  $.pulse.impulse = function($obj) {
    var opts = {
      speed: 2500,
      pulses: 10,
      fadeLow: 0.2,
      fadeHigh: 0.8
    };
    doPulse($obj,opts);
  }

  // Static Function
  $.pulse.warpspeed = function($obj) {
    var opts = {
      speed: 25,
      pulses: 100,
      fadeLow: 0.2,
      fadeHigh: 0.8
    };
    doPulse($obj,opts);
  }

  // Pulse plugin default options
  $.fn.pulse.defaults = {
    speed: "slow",
    pulses: 2,
    fadeLow: 0.2,
    fadeHigh: 1
  };
})(jQuery);

Calling the static methods available in your plugin is very straightforward, requiring only that you explicitly pass a valid object to operate on:

// Call the impulse method on the first element returned
jQuery.pulse.impulse(jQuery('p:first'));

// Call the warpspeed method on the first element returned
jQuery.pulse.impulse(jQuery('p:first'));

Discussion

Adding a static function within the scope of your plugin only requires adding a way for code outside of your plugin to call it. This is accomplished by attaching the functions to the jQuery object.

In the previous example, we’ve added a namespace object to aid in organizing our code better. If all that your plugin required was a single static function, it would be completely appropriate to expose your static function without adding a namespacing object. After adding our namespace object, we simply define our functions like normal and attach them to the namespace object we created. Doing this exposes our function to the global namespace, while allowing the contents of the functions to access private functions and variables.

Taking advantage of the static function is as simple as calling it using the jQuery object we attached it to. This function is called without utilizing jQuery selectors, so in order to operate on a DOM element, that element must be explicitly passed to the function.

A static function attached to the jQuery object is another example of the flexibility of the jQuery library. Your entire plugin could be made up of adding static functions that simply extend the jQuery core in interesting new ways. A static function could be the entry point you provide to your plugin, or it could be a simple shortcut method you’ve found useful that’s packaged in your plugin in a way that makes it easier to share with other developers. Whatever the need, static functions can be a useful and powerful tool when building your own jQuery plugin.

12.9. Unit Testing Your Plugin with QUnit

Problem

You want to raise the quality and reliability of your jQuery plugin by creating unit tests for it. How do you write and ship tests with your jQuery plugin?

Solution

The easiest method to write unit tests for a jQuery plugin is to utilize QUnit, the same unit testing framework that the jQuery project uses. With QUnit, you can write your tests right in JavaScript and ship them with your plugin for your users to run in their own browsers:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <script type="text/javascript" src="../jquery-1.3.2.min.js"></script>
  <script type="text/javascript" src="metadata/jquery.metadata.js"></script>
  <script type="text/javascript" src="jquery.pulse.js"></script>

  <link rel="stylesheet" 
href="http://jqueryjs.googlecode.com/svn/trunk/qunit/testsuite.css" type="text/css" 
media="screen" />
  <script type="text/javascript" 
src="http://jqueryjs.googlecode.com/svn/trunk/qunit/testrunner.js"></script>
</head>
<body>

  <script type="text/javascript">
    module("Testing the jQuery Pulse Plugin");
    test("Test Pulse with basic options", function() {
      $("div.starship").pulse();

      equals($("#enterprise").css("opacity"),1,"The element should be visible");
      equals($("#galactica").css("opacity"),1,"The element should be visible");
    });

    test("Test Impulse", function() {
      $.pulse.impulse($("#galactica"));

      equals($("#galactica").css("opacity"),1,"The element should be visible");
    });

    test("Test Warp Speed", function() {
      $.pulse.warpspeed($("#enterprise"));

      equals($("#enterprise").css("opacity"),1,"The element should be visible");
    });
  </script>

  <div id="main">
    <div class="starship" id="enterprise">USS Enterprise - NC-1701-A</div>
    <div class="starship" id="galactica">Battlestar Galactica</div>
  </div>

</body>
</html>

Discussion

Learning how to effectively test code is beyond the scope of this chapter. The tests written in the previous example are intended to simply show an example of what can be done with unit testing. Chapter 18 goes into great detail about unit testing and specifically the QUnit framework. For a discussion on how to use QUnit, what types of things you can test, and how to effectively test your code, please refer to that chapter.

Shipping unit tests with your plugin is another great way to show developers you are committed to the success and stability of the code that you publish. This builds trust with your user base and shows that your plugin is a good member of the jQuery plugin ecosystem. Tests also make it easy for users of your plugins to find bugs that can creep up in another runtime environment, such as a different browser. This allows you, the plugin author, to better address the bug that was found by having a working test bed, allowing you, the plugin developer, to directly address the bug that was found.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.107.254