Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

buster-amd

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

buster-amd

Extension for testing AMD modules

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16
decreased by-38.46%
Maintainers
2
Weekly downloads
 
Created
Source

buster-amd

A plugin for BusterJS that allows you to use an AMD loader to test asynchronous modules. You must provide your own loader. By default, a loader that provides require(deps, callback) is assumed. This will eventually be pluggable.

Install

Installation is done using npm:

$ npm install buster-amd

Usage

Load in your configuration file, specifying your loader and the required configuration file as libs. You also need to add the buster-amd extension to your configuration::

    var config = module.exports;
    
    config["Browser tests"] = {
        rootPath: "../",
        libs: [
            "libs/require.js",
            "requirejs-config.js"
        ],
        sources: ["src/**/*.js"],
        tests: ["test/**/*.js"],
        extensions: [require("buster-amd")]
    };
  • You should list your tests and sources as normal. Your sources must be specified in the configuration even if you will require them from your tests, otherwise, Buster will not make them available on the test server.

If you have any issues with paths for your required files, check the configuration section below.

Your tests will drive the show. To run tests with the AMD extension, your tests should be wrapped in a call to define, which pulls in dependencies (i.e. your modules) and in the callback define specs/test cases as usual::

    define(['moduleToTest.js'], function(moduleToTest){
        buster.testCase("A test case", {
            "test the module": function(){
                assert.isObject(moduleToTest);
            }
        });
    });

Configuration

The AMD extension has one configuration option: a path mapper. The path mapper is an optional function that translates Buster.JS paths (which are absolute, e.g. /test/my-test.js) to AMD friendly module IDs.

The default mapper converts /test/my-test.js to test/my-test, i.e. strips leading slash and file suffix::

    function (path) {
        return path.replace(/\.js$/, "").replace(/^\//, "");
    }

However, if your AMD loader specifies a basePath in its configuration the default mapper might cause you issues::

    require.config({
      baseUrl: 'src/'
    });

In this case, every module your loader attempts to load will be prefixed with this basePath::

    src/test/my-test.js

You don't need to restructure your project to solve this issue. If your tests live outside of that directory, you can fix that with a different mapping function::

    config["Browser tests"] = {
        rootPath: "../",
        libs: [
            "libs/require.js",
            "requirejs-config.js"
        ],
        sources: ["src/**/*.js"],
        tests: ["test/**/*.js"],
        extensions: [require("buster-amd")],
        "buster-amd": {
            pathMapper: function (path) {
              // prefix any path starting with a slash with ../
              return path.replace(/\.js$/, "").replace(/^\//, "../");
            }
        }
    };

In this case, your AMD loader will load the files with the following path in the browser with the path src/../test/my-test.js which is equivalent to test/my-test.js

  • If you specify your own mapper and decide not to remove the file extension make sure you understand how your loader deals with files with an extension. require.js for instance will load them with an absolute path, not prefixing these with a baseUrl option, but curl.js will treat these files as any other modules.

Another example: use the following mapper for AMD loader plugins::

    var config = module.exports;
    
    config["Browser tests"] = {
        rootPath: "../",
        sources: ["src/**/*.js"],
        tests: ["test/**/*.js"],
        extensions: [require("buster-amd")],
        "buster-amd": {
            pathMapper: function (path) {
                return "plugin!" + path.replace(/^\//, "").replace(/\.js$/, "");
            }
        }
    };

Examples

Check the demos repository for example projects.

FAQs

Package last updated on 23 Sep 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc