Giter Club home page Giter Club logo

grunt-template-jasmine-requirejs's Introduction

RequireJS template for Jasmine unit tests Build Status

Installation

By default, this template works with Jasmine 2.x

npm install grunt-template-jasmine-requirejs --save-dev

Support for both Jasmine 1.x and 2.x

You'd install ~0.1 version of this template if your test specs are based on Jasmine 1.x

npm install grunt-template-jasmine-requirejs@~0.1 --save-dev

Options

vendor

Type: String|Array

Works same as original. But they are loaded before require.js script file

helpers

Type: String|Array

Works same as original. But they are loaded after require.js script file

Template Options

templateOptions.version

Type: String Options: 2.0.0 to 2.1.10 or path to a local file system version(relative to Gruntfile.js). Absolute path is allowed as well. Default: latest requirejs version included

The version of requirejs to use.

templateOptions.requireConfigFile

Type String or Array

This can be a single path to a require config file or an array of paths to multiple require config files. The configuration is extracted from the require.config({}) call(s) in the file, and is passed into the require.config({}) call in the template.

Files are loaded from left to right (using a deep merge). This is so you can have a main config and then override specific settings in additional config files (like a test config) without having to duplicate entire requireJS configs.

If requireConfig is also specified then it will be deep-merged onto the settings specified by this directive.

templateOptions.requireConfig

Type: Object

This object is JSON.stringify()-ed ( support serialize Function object ) into the template and passed into var require variable

If requireConfigFile is specified then it will be loaded first and the settings specified by this directive will be deep-merged onto those.

templateOptions.specRunnerTemplate

Type: String

If you wish to override the template version of the SpecRunner.html that is built out (for customizing), then you can provide the path for this. If you wish to alter the template, you can create a copy from here. If specRunnerTemplate is provided and the file exists, it uses this file, otherwise it falls back to the default built in template.

templateOptions.boot

Type: String

If you wish to override the default requirejs-boot.js script with the RequireJS support that is built out (for customizing), then you can provide the path for this. If you wish to alter the template, you can create a copy from here. If the boot option is provided and the file exists, it will use this file, otherwise it will fall back to the default built-in script.

Sample usage

// Example configuration using a single requireJS config file
grunt.initConfig({
  connect: {
    test : {
      port : 8000
    }
  },
  jasmine: {
    taskName: {
      src: 'src/**/*.js',
      options: {
        specs: 'spec/*Spec.js',
        helpers: 'spec/*Helper.js',
        host: 'http://127.0.0.1:8000/',
        template: require('grunt-template-jasmine-requirejs'),
        templateOptions: {
          requireConfigFile: 'src/main.js'
        }
      }
    }
  }
});
// Example configuration using an inline requireJS config
grunt.initConfig({
  connect: {
    test : {
      port : 8000
    }
  },
  jasmine: {
    taskName: {
      src: 'src/**/*.js',
      options: {
        specs: 'spec/*Spec.js',
        helpers: 'spec/*Helper.js',
        host: 'http://127.0.0.1:8000/',
        template: require('grunt-template-jasmine-requirejs'),
        templateOptions: {
          requireConfig: {
            baseUrl: 'src/',
            paths: {
              "jquery": "path/to/jquery"
            },
            shim: {
              'foo': {
                deps: ['bar'],
                exports: 'Foo',
                init: function (bar) {
                  return this.Foo.noConflict();
                }
              }
            },
            deps: ['jquery'],
            callback: function($) {
              // do initialization stuff
              /*

              */
            }
          }
        }
      }
    }
  }
});
// Example using a base requireJS config file and specifying
// overrides with an inline requireConfig file.
grunt.initConfig({
  connect: {
    test : {
      port : 8000
    }
  },
  jasmine: {
    taskName: {
      src: 'src/**/*.js',
      options: {
        specs: 'spec/*Spec.js',
        helpers: 'spec/*Helper.js',
        host: 'http://127.0.0.1:8000/',
        template: require('grunt-template-jasmine-requirejs'),
        templateOptions: {
          requireConfigFile: 'src/main.js',
          requireConfig: {
            baseUrl: 'overridden/baseUrl',
            shim: {
              // foo will override the 'foo' shim in main.js
              'foo': {
                deps: ['bar'],
                exports: 'Foo'
              }
            }
          }
        }
      }
    }
  }
});
// Example using a multiple requireJS config files. Useful for
// testing.
grunt.initConfig({
  connect: {
    test : {
      port : 8000
    }
  },
  jasmine: {
    taskName: {
      src: 'src/**/*.js',
      options: {
        specs: 'spec/*Spec.js',
        helpers: 'spec/*Helper.js',
        host: 'http://127.0.0.1:8000/',
        template: require('grunt-template-jasmine-requirejs'),
        templateOptions: {
          requireConfigFile: ['src/config.js', 'spec/config.js']
          requireConfig: {
            baseUrl: 'overridden/baseUrl'
          }
        }
      }
    }
  }
});

Note the usage of the 'connect' task configuration. You will need to use a task like grunt-contrib-connect if you need to test your tasks on a running server.

RequireJS notes

If you end up using this template, it's worth looking at the source in order to familiarize yourself with how it loads your files. The load process consists of a series of nested require blocks, incrementally loading your source and specs:

require([*YOUR SOURCE*], function() {
  require([*YOUR SPECS*], function() {
    require([*GRUNT-CONTRIB-JASMINE FILES*], function() {
      // at this point your tests are already running.
    }
  }
}

If "callback" function is defined in requireConfig, above code will be injected to the end of body of "callback" definition

templateOptions: {
  callback: function() {
    // suppose we define a module here
    define("config", {
      "endpoint": "/path/to/endpoint"
    })
  }
}

Generated runner page with require configuration looks like:

var require = {
  ...
  callback: function() {
    // suppose we define a module here
    define("config", {
      "endpoint": "/path/to/endpoint"
    })

    require([*YOUR SOURCE*], function() {
      require([*YOUR SPECS*], function() {
        require([*GRUNT-CONTRIB-JASMINE FILES*], function() {
          // at this point your tests are already running.
        }
      }
    }
  }
  ...
}

This automation can help to avoid unexpected dependency order issue

Change Log

  • v0.2.3 Fixed path issues #77
  • v0.2.2 Fixed regression which casued by #65
  • v0.2.1 Fixed #65
  • v0.2.0 Added Jasmine 2 support
  • v0.1.10 03.14.14, Fixed #53, #52, #46, #36 wrong path error when runner outfile is specified at elsewhere
  • v0.1.9, 02.04.14, #57 prevents conflict with grunt-contrib-jasmine 0.6.x, added requirejs 2.1.9 & 2.1.10

Authors / Maintainers

  • Jarrod Overson (@jsoverson)
  • Cloud Chen (@cloudchen)

grunt-template-jasmine-requirejs's People

Contributors

albertopq avatar asaayers avatar cjuarez avatar cloudchen avatar davyboyhayes avatar egeste avatar ejdyksen avatar existentialism avatar jamespamplin avatar jsoverson avatar korprulu avatar larsonjj avatar lukeapage avatar ohgyun avatar prantlf avatar raffishquartan avatar reinseth avatar ross-hunter avatar timsnadden avatar tmartensen avatar zdennis avatar

Watchers

 avatar  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.