Giter Club home page Giter Club logo

buster-amd's Introduction

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.

buster-amd's People

Contributors

cjohansen avatar dominykas avatar eskimoblood avatar johlrogge avatar mroderick avatar pieterv avatar trodrigues avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.